2015-02-24 79 views
-1

我有一个应用程序,其中包括一个刷新按钮在操作栏中。当我第二次点击它,我的应用程序去调试视图,它说我的asyncClass对象还没有完成!取消asynctask强制不起作用

请让我知道如何解决我的问题。

MyActivity:

public class MyActivity extends ActionBarActivity { 

    private getAllDataFromServer asyncClass;  

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    asyncClass = new getAllDataFromServer(); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
    // TODO Auto-generated method stub 
    if(item.getItemId() == R.id.action_update) 
     if(asyncClass.getStatus() == Status.FINISHED || asyncClass.getStatus() == Status.RUNNING) 
     { 
      asyncClass.cancel(true); 
     } 
     if(asyncClass.getStatus() == Status.PENDING) 
     { 
      asyncClass.execute(); 
     } 
    } 
    return super.onOptionsItemSelected(item); 
} 

getAllDataFromServer:

private class getAllDataFromServer extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     pDialog = new ProgressDialog(SendCommentsHistoryActivity.this); 
     pDialog.setMessage("Be patient ..."); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
     super.onPreExecute(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     if(isCancelled()) return null; 
     jObject = jParser.getJSONFromUrl(TAG_URL, param); 
     Log.i("JSONNNNNNNNN", jObject.toString()); 
     try { 
      // get json array 

      for (int i = 0; i < jArray.length(); i++) 
      { 
       // get jsonObjects and fill database 
      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     fillListView(); 
     pDialog.dismiss(); 
     super.onPostExecute(result); 
    } 

    @Override 
    protected void onCancelled() { 
     // TODO Auto-generated method stub 
     super.onCancelled(); 
    } 
} 

UPDATE: JSONParser类:

public class JSONParser { 
static InputStream is = null; 
static JSONObject jObj = null; 
static String json = ""; 

public JSONParser() { 
} 

/** 
* Send POST Data to Server & Retrieve JSON From URL 
*/ 
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) { 
// Making HTTP request 
try { 
    // defaultHttpClient 
    DefaultHttpClient httpClient = new DefaultHttpClient(); 
    HttpPost httpPost = new HttpPost(url); 
    httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8")); 
    HttpResponse httpResponse = httpClient.execute(httpPost); 
    HttpEntity httpEntity = httpResponse.getEntity(); 
    is = httpEntity.getContent(); 
} catch (UnsupportedEncodingException e) { 
    e.printStackTrace(); 
} catch (ClientProtocolException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
try { 
    BufferedReader reader = new BufferedReader(new InputStreamReader(
      is, "UTF8"), 8); 
    StringBuilder sb = new StringBuilder(); 
    String line = null; 
    while ((line = reader.readLine()) != null) { 
     sb.append(line + "\n"); 
    } 
    is.close(); 
    json = sb.toString(); 
} catch (Exception e) { 
    Log.e("Buffer Error", "Error converting result " + e.toString()); 
} 
try { 
    jObj = new JSONObject(json); 
} catch (JSONException e) { 
    Log.e("JSON Parser", "Error parsing data " + e.toString()); 
} 
// return JSON Object 
return jObj; 
} 

} 

错误的屏幕截图: enter image description here

任何建议将不胜感激...

+1

显示刷新按钮 – 2015-02-24 06:43:23

+1

的代码问题是AsyncTask取消不取消doInBackground进程。您必须在doInBackground流程中添加条件来检查流程是否已取消 – 2015-02-24 06:49:21

+0

我更新了我的代码。请再次检查亲爱的... – 2015-02-24 06:51:10

回答

0

一旦尝试如下

if(!asyncClass.isCancelled()) { 
     asyncClass.cancel(true); 
} 

和doInBackground()的for循环写

@Override 
protected Void doInBackground(Void... params) { 
    ....................... 
     // get json array 

     for (int i = 0; i < jArray.length(); i++) 
     { 
      // get jsonObjects and fill database 

      // Escape early if cancel() is called 

      if (isCancelled()) break; 
     } 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

希望这会帮助你。

+0

我做到了,但它没有'解决我的问题。 – 2015-02-24 07:00:27

+0

@FarshadKazemi我编辑了我的答案一次检查你只有这样试过吗? – 2015-02-24 07:02:44

+0

如果(!asyncClass.isCancelled())asyncClass.cancel(true);我使用您的代码并将onOptionItemSelected节更改为\t \t \t \t; ayncClass.execute();但我再次得到错误 – 2015-02-24 07:07:20