强制停止httpClient httpget?

时间:2013-10-01 16:44:01

标签: android android-asynctask

目标根本不能同时停止httpClienthttpget

当我点击进度对话框cancel按钮时,RequestAsyncTask将停止并中止,但不是,请求仍在运行,直到完成

这是我的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();   
        }


    }   
  }

任何建议请如何在android !!

中完全停止获取或发布http请求

1 个答案:

答案 0 :(得分:6)

AsyncTask.cancel()方法与取消http请求无关。 cancel(true)仅阻止在doInBackground完成作业后调用onPostExecute()。而是将调用onCancelled()。

如果您想取消http请求,请使用HttpClient.getConnectionManager().shutdown();

      public void onClick(DialogInterface dialog, int which) {
          cancel(true);
          httpClient.getConnectionManager().shutdown();
          finish();
        }});