2012-12-13 70 views
-1

下面是我創建的一個異步類,我嘗試在完成時執行一個對話框和一個Toast。asynTask沒有顯示對話框和烤麪包片

永遠不會出現烤麪包或對話。

我asykTask:

public class EmailPictureService extends HTTPRequestAsyncTask { 
    Context context; 
    ProgressDialog dialog; 
    public EmailPictureService(Context context){ 
     this.context = context; 
     //dialog = new ProgressDialog(context); 
    } 
    @Override 
    protected void onPreExecute() { 

     super.onPreExecute(); 
    } 

    @Override 
    protected String doInBackground(Object... params) { 
     Log.v("Start EMAIL SERVICE","START YOPPPPPPPPPPPPPPPPPPPPPP!"); 
     dialog = new ProgressDialog(context); 
     dialog.setMessage("Sending..."); 
     dialog.setIndeterminate(true); 
     dialog.show(); 
     HTTPInvoker invoker = new HTTPInvoker(); 
     HttpResponse response = null; 

     EmailPicture emailPicture = new EmailPicture(); 
     emailPicture.setDeviceType("TABLET"); 
     emailPicture.setStoreId((String)params[1]); 
     emailPicture.setNotificationType("E"); 
     emailPicture.setRecipientName((String)params[2]); 
     emailPicture.setRecipientEmail((String)params[3]); 

     String jsonString = JSONConverter.toJson(emailPicture); 
     response = invoker.invokePOSTFileService((String)params[0], jsonString, (File[])params[4]); 
     return parseHttpResponse(response); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     String msg = ""; 
     if (dialog.isShowing()) { 
      dialog.dismiss(); 
     } 
     if (result != null) { 
      JSONObject jsonObject = null; 
      long errorCode = 0; 
      try { 
       jsonObject = new JSONObject((String) result); 
       errorCode = jsonObject.getLong("errorCode"); 
       if(errorCode<1){ 
        msg ="Success, your picture has been sent"; 
       }else{ 
        msg = "Sorry, there was an error sending your picture. Please try again later."; 
       } 
       Log.i(Constants.TAG, "Error Code...." + errorCode); 
       Toast toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT); 
       toast.show(); 

      } catch (JSONException e1) { 
       Log.i(Constants.TAG, "Exception...." + e1); 
       Toast toast = Toast.makeText(context, "Failure: "+e1, Toast.LENGTH_SHORT); 
       toast.show(); 
       e1.printStackTrace(); 
      } 
     } 

    } 

} 

我如何把它從我的活動:

new EmailPictureService(this).execute(url,storeID,cusName, cusEmail, new File[]{file}); 

我的日誌 enter image description here

回答

1

你不應該試圖進入電影從doInBackground()的UI。 AsyncTasks和doInBackground()的目的是避免陷入泥淖UI線程......相反,你應該在瓶坯適當的方法在UI工作:onPreExecute()onProgressUpdate()onPostExecute()

0

我注意到,你在實例化的doInBackground progressDialog() 。請將其移至onPreExecute()。 doInBackground()只能用於非UI工作。 =)

這應該「解決」你的問題。

1

我懷疑吐司沒有顯示,因爲你的結果總是爲空。您的日誌在帖子上顯示錯誤

正如其他人所說,從onPreExecute()啓動進度對話框()