1

目标根本无法同时停止 httpClientANDhttpget

当我单击进度对话框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();   
        }


    }   
  }

任何建议请如何http在android中完全停止获取或发布请求!

4

1 回答 1

6

The AsyncTask.cancel() method has nothing to do with the cancel of a http request. cancel(true) only prevents that onPostExecute() will be invoked after doInBackground finished the job. Instead onCancelled() will be called.

If you wanna cancel a http request use HttpClient.getConnectionManager().shutdown();

      public void onClick(DialogInterface dialog, int which) {
          cancel(true);
          httpClient.getConnectionManager().shutdown();
          finish();
        }});
于 2013-10-01T16:53:21.807 回答