2012-12-19 77 views
1

我想在json請求帶有空值的時候取消AsyncTask, 並顯示Toast消息。如何在運行狀態中取消AsyncTask。

private class PostTask extends AsyncTask<String, Integer, String> 
{ 
    //Before running code in the separate thread 
    @Override 
    protected void onPreExecute() 
    { 

     progressDialog = new ProgressDialog(login.this); 
     progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
     progressDialog.setMessage("please wait..."); 
     progressDialog.setCancelable(false); 
     progressDialog.setIndeterminate(false); 
     progressDialog.setMax(100); 
     progressDialog.setProgress(0); 
     progressDialog.show(); 
    } 
    //The code to be executed in a background thread. 
    @Override 
    protected String doInBackground(String... params) 
    { 
     String url=params[0]; 
     Parser parse = new Parser(); 
     try{  
      String email = textinput.getText().toString(); 
      String pass = password.getText().toString(); 
      JSONObject JsonString = parse.getJSONFromUrl(url,email,pass); 

      //String email = JsonString.getString("email"); 
      Constants.ID = JsonString.getString("id"); 

     } 
     catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       } 

     catch (NullPointerException e){ 

      publishProgress(1); 
      //Toast.makeText(login.this, "Invalid credentials", Toast.LENGTH_LONG).show(); 
     } 
     return "All Done!"; 
    } 
    //Update the progress 
    @Override 
    protected void onProgressUpdate(Integer... values) 
    { 
     //set the current progress of the progress dialog 
    // progressDialog.setProgress(values[0]); 
     //Toast.makeText(login.this, "Invalid credentials", Toast.LENGTH_LONG).show(); 
    } 
    //after executing the code in the thread 
    @Override 
     protected void onPostExecute(String result) { 

      super.onPostExecute(result); 
      startActivity(new Intent("com.example.mysampleapp.DASHBOARDTAB")); 
      progressDialog.dismiss();  
    } 
} 

是否有任何取消方法來取消async任務,如果我在JSON請求獲得空和從那裏我打電話了AsyncTask同一活動顯示消息?

+0

檢查此鏈接http://stackoverflow.com/questions/4748964/ android-cancel-asynctask-forcefully?lq = 1 – 2012-12-19 12:14:02

+0

[Android:取消異步任務]的可能重複(http://stackoverflow.com/questions/6039158/android-cancel-async-task) – JaredMcAteer

+0

取消異步任務的示例http://www.quicktips.in/correct-way-to-cancel-an-asynctask- in-android/ –

回答

2

嘗試,如果JSON是空返回錯誤代碼,類似:

catch (NullPointerException e){ 

     publishProgress(1); 
     return "Error"; 
    } 

然後在onPostExecute()

@Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 

     if(result.equals("Error")) { 
      //Error 
     } else { 
      startActivity(new Intent("com.example.mysampleapp.DASHBOARDTAB")); 
      progressDialog.dismiss();  
     } 
} 
+0

superthanks alot –

+0

取消一個asynctask的例子http://www.quicktips.in/correct-way-to-cancel-an-asynctask-in-android/ –

1

試試這個:

postTask = new PostTask(); 

if (postTask != null) 
{ 
    postTask.cancel(true); 
    postTask = null; 
} 

感謝。

相關問題