目标根本无法同时停止 httpClient
ANDhttpget
当我单击进度对话框cancel
按钮时Request
,AsyncTask
应停止并中止但不是,请求仍在运行直到完成
这是我的AsyncTask
测试代码
public class getAlbuminfo extends AsyncTask<String,String, String> {
ProgressDialog progressDialog;
JSONObject data = null;
boolean prependMore = false;
//Http
HttpClient httpClient;
HttpGet httpget;
/**********
* Begien
*/
@SuppressWarnings("deprecation")
@Override
protected void onPreExecute() {
if ( onlypagenat == false )
{
progressDialog = new ProgressDialog(context);
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.setMessage(context.getResources().getString(R.string.opening_album));
progressDialog.setButton(context.getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
cancel(true);
finish();
}});
progressDialog.show();
}
prependMore = appendMore;
appendMore = false;
}
/**************
* IF Canceled
*/
@Override
protected void onCancelled() {
Log.i("Http Response:", "Aborted");
if ( httpget != null )
{
httpClient.getConnectionManager().shutdown();
httpget.abort();
}
}
/****************
* Make Request
*/
@Override
protected String doInBackground(String... params) {
String response = null;
String url = Api.foruminfo.forum_api_url+"/album.php?albumid="+albumid+"&page="+page+"&onlypagenat="+(onlypagenat?1:0);
httpClient = new DefaultHttpClient();
httpget = new HttpGet(url);
//Add Values User Headers and Cookies
httpget.addHeader("Cookie", Api.forumCookies());
httpget.setHeader("Accept-Charset", "utf-8");
// Making HTTP Request
try {
HttpResponse serverResponse = httpClient.execute(httpget);
Log.i("Http HttpResponse:", serverResponse.toString());
//Set Response
response = EntityUtils.toString(serverResponse.getEntity());
} catch (ClientProtocolException e) {e.printStackTrace();
} catch (IOException e) { e.printStackTrace();}
return response;
}
/**********************
* Done
*/
@Override
protected void onPostExecute(String response) {
Log.i("Http HttpResponse:", response);
if ( onlypagenat == false )
{
progressDialog.dismiss();
}
}
}
任何建议请如何http
在android中完全停止获取或发布请求!