2017-08-30 26 views
0

可以在一個asynctask中擁有多個httpclient嗎?在我的代碼如下..它檢索用戶的信息,然後將其顯示到一個新的活動..但我也想獲得追隨者的數量和用戶時ProfileAsync執行..我有單獨的PHP文件的的用戶信息和追隨者及以下的號碼檢索..異步任務(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

是的,你可以發送多個URL作爲參數,可以將所有網址將可作爲參數數組一樣PARAMS [0],則params [1]和你可以通過它循環發送多個請求,但我建議不要在單個AyncTask中執行該操作,因爲這需要大量時間,並且如Android Developer指南中所述,AsyncTask應該用於需要工作線程的短操作,您可以在這裏閱讀 - https://developer.android.com/reference/android/os/AsyncTask.html

如果您想要發送多個請求,請嘗試使用着名的網絡庫(如翻新),或者如果您想使用AsynTask,則爲每個請求編寫一個單獨的任務。