是否可以在asynctask中包含多个httpclient?在我的下面的代码..它检索用户的信息,然后将其显示为一个新的活动..但我也想在执行ProfileAsync时获得关注者的数量和用户的跟踪...我有单独的php文件检索用户信息以及关注者和关注者的数量..
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
答案 0 :(得分:1)
是的,你可以发送多个网址作为参数,所有的网址都可以作为params数组,如params [0],params [1],你可以循环通过它发送多个请求,但我建议不要在单个AyncTask,因为它将花费大量时间并且如Android Developer指南中所述,AsyncTask应该用于需要工作线程的简短操作,您可以在这里阅读它 - https://developer.android.com/reference/android/os/AsyncTask.html
如果您想发送多个请求尝试使用着名的网络库(如改造)或者如果您想使用AsynTask,请为每个请求编写单独的任务。