2017-08-30 11 views
0

asynctask内に複数のhttpclientを置くことは可能ですか?下のコードでは、ユーザーの情報を取得して新しいアクティビティに表示します。しかし、ProfileAsyncが実行されたときに、フォロワーの数を取得し、ユーザーのフォローもしたいと思っていました。ユーザ情報やフォロワーと以下の数の検索...非同期タスクの複数の操作(Android)

class ProfileAsync extends AsyncTask<String, String, String>{ 

       private Dialog loadingDialog; 

       @Override 
       protected void onPreExecute() { 
        super.onPreExecute(); 
        loadingDialog = ProgressDialog.show(HomePageActivity.this, "Please wait", "Loading..."); 
       } 

       @Override 
       protected String doInBackground(String... params) { 
        String json=null; 
        String json2=null; 
        byte[] data; 
        StringBuffer buffer = null; 
        InputStream is = null; 



        try{ 
         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
         nameValuePairs.add(new BasicNameValuePair("Username", uname)); 

         HttpClient httpClient = new DefaultHttpClient(); 
         HttpPost httpPost = new HttpPost(PROFILE_URL); 
         httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

         HttpResponse response = httpClient.execute(httpPost); 

         HttpEntity entity = response.getEntity(); 
         json = EntityUtils.toString(entity); 
         Log.e("Profile JSON: ", json.toString()); 

        } catch (ClientProtocolException e) { 
         e.printStackTrace(); 
        } catch (UnsupportedEncodingException e) { 
         e.printStackTrace(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
        return json; 
       } 

       @Override 
       protected void onPostExecute(String json){ 
        super.onPostExecute(json); 

        loadingDialog.dismiss(); 

        try 
        { 
        jsonobject = new JSONObject(json); 
        jsonarray = jsonobject.getJSONArray("user"); 
        JSONObject jb= jsonarray.getJSONObject(0); 
        //Username = jb.getString("Username"); 
        Password = jb.getString("Password"); 
        Fullname = jb.getString("Fullname"); 
        Email = jb.getString("Email"); 
        Bio = jb.getString("Bio"); 
        Location = jb.getString("Location"); 



        fn.setText(Fullname); 
        em.setText(Email); 
        loc.setText(Location); 
        b.setText(Bio); 

        if(json!=null) 
        { 
         Intent i = new Intent(HomePageActivity.this,ProfileActivity.class); 
         i.putExtra(u.username(), u.getUsername()); 
         i.putExtra("password",Password); 
         i.putExtra("fullname",Fullname); 
         i.putExtra("email", Email); 
         i.putExtra("bio", Bio); 
         i.putExtra("location", Location); 
         startActivity(i); 
         finish(); 
        } 



        }catch(Exception e) 
        { 
         e.printStackTrace(); 
        } 



       } 
      }//end of profileasynctask 

答えて

1

はい、あなたはのparamsとして、複数のURLを送信することができ、すべてのURLは、[0]のparamsのようなのparams配列として利用できる意志のparams [1]とあなた複数のリクエストを送信するためにループすることができますが、1つのAyncTaskでそれを実行しないことをお勧めします.Androidタスクは、ここで読むことができます - https://developer.android.com/reference/android/os/AsyncTask.html

複数のリクエストを送信する場合は、改造などの有名なネットワークライブラリを使用してみるか、AsynTaskを使用する場合は、リクエストごとに別々のタスクをコーディングします。