我有一個應用程序,其中包括一個刷新按鈕在操作欄中。當我第二次點擊它,我的應用程序去調試視圖,它說我的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;
}
}
錯誤的屏幕截圖:
任何建議將不勝感激...
顯示刷新按鈕 – 2015-02-24 06:43:23
的代碼問題是AsyncTask取消不取消doInBackground進程。您必須在doInBackground流程中添加條件來檢查流程是否已取消 – 2015-02-24 06:49:21
我更新了我的代碼。請再次檢查親愛的... – 2015-02-24 06:51:10