2014-04-23 43 views
0

大家好我有麻烦取消进度对话框,如果任何异常发生在我的AsyncTask中的doInBackground,因为它永远不会到达onPostExecute并从不关闭制作ANR的进度对话框。关闭进度对话框和处理AsyncTask中的异常

下面是的AsyncTask代码

private class checkAS extends AsyncTask<Void, Void, Void> 
{ 
    ProgressDialog dialogue; 
    @Override 
    protected void onPostExecute() { 
     // TODO Auto-generated method stub 
     super.onPostExecute(); 
     dialogue.dismiss(); 

    } 
    @Override 
    protected Void doInBackground(Void... params) { 
     //Long Network Task 
     return null; 
    } 
    @Override 
    protected void onPreExecute(Void result) { 
     // TODO Auto-generated method stub 
     super.onPreExecute(result); 
     dialogue = new ProgressDialog(MainActivity.this); 
     dialogue.setTitle("Processing"); 
     dialogue.setMessage("Getting Profile Information"); 
     dialogue.setIndeterminate(true); 
     dialogue.setCancelable(false); 
     dialogue.show(); 
    } 
} 

我的问题是,如果出现任何异常,在doInBackground我将如何处理它,onPostExecute怎么会被要求关闭该对话,因为我不能驳回doInBackground。如何同步它?

+0

onPreExecute()和dialog.dismiss()onPostExecute设置进度条对话框的所有属性。 –

回答

0

您正在onPostExecute方法中创建对话框对话框,它应该在onPreExecute方法中。 试试这个。

 private class checkAS extends AsyncTask<Void, Void, Void> 
    { 
     ProgressDialog dialogue; 
     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 

dialogue = new ProgressDialog(MainActivity.this); 
      dialogue.setTitle("Processing"); 
      dialogue.setMessage("Getting Profile Information"); 
      dialogue.setIndeterminate(true); 
      dialogue.setCancelable(false); 
      dialogue.show(); 

     } 
     @Override 
     protected Void doInBackground(Void... params) { 
      //Long Network Task 
      return null; 
     } 
     @Override 
     protected void onPostExecute(Void result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 
dialogue.dismiss(); 

     } 
    } 
+0

对不起,发布了......我现在创建了这个代码,只是为了演示。在原始代码中,它在onPreExecute中。现在编辑。 – Saty

1

试试这个..

返回类似从doInBackground字符串。如果Exception来到catch是指定字符串值error否则返回success

private class checkAS extends AsyncTask<Void, Void, String> 
{ 
    ProgressDialog dialogue; 
    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 
     dialogue = new ProgressDialog(MainActivity.this); 
     dialogue.setTitle("Processing"); 
     dialogue.setMessage("Getting Profile Information"); 
     dialogue.setIndeterminate(true); 
     dialogue.setCancelable(false); 
     dialogue.show(); 

    } 
    @Override 
    protected String doInBackground(Void... params) { 
     //Long Network Task 
     String result; 
     try{ 
      result = "success" 
     } 
     catch(Exception e){ 
      result = "error"; 
     } 
     return result; 
    } 
    @Override 
    protected void onPostExecute(String result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
     if(result.equals("error"))   
      dialogue.dismiss(); 
     else 
      // do something 
    } 
} 
+0

我应该在doninBackground中返回null还是应该返回结果? – Saty

+0

@Saty抱歉编辑你应该返回结果。 – Hariharan

+0

是的,这是正确的一个后台操作,但如果我将不得不在一个循环中调用相同的AsyncTask 4或5次,进度对话的实例的数量将增加,因此使应用程序不响应。顺便说一句,你是同一个哈里哈兰谁唱歌????? – Saty

0
@Override 
    protected String doInBackground(String... params) 
    { 
     System.out.println("check user profile"); 
     try 
     { 

     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
      publishProgress((e.getMessage())); 
     } 
     return result; 

    } 

    @Override 
    protected void onProgressUpdate(String... values) 
    { 
     // TODO Auto-generated method stub 
     super.onProgressUpdate(values); 
     Toast.makeText(activity, values[0], Toast.LENGTH_LONG); 

      if(dialog != null && dialog.isShowing()) 
       dialog.dismiss(); 

    } 

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

      if(dialog != null && dialog.isShowing()) 
      { 
       dialog.dismiss(); 
      } 
     } 
0

您可能希望在try catch结构的finally块驳回对话框。

try { 
... 
} catch { 
... 
finally{ 
//dismiss dialog here. 
} 
0

首先检查你是否在对话框中显示或不使用此代码可以检查

if(dialog.isShowing()) 
dialog.dismiss(); 

而且使用异常处理,以避免未知异常

0
private class checkAS extends AsyncTask<String, Integer, String> { 

        public static final int POST_TASK = 1; 

        private static final String TAG = "checkAS"; 

        // connection timeout, in milliseconds (waiting to connect) 
        private static final int CONN_TIMEOUT = 12000; 

        // socket timeout, in milliseconds (waiting for data) 
        private static final int SOCKET_TIMEOUT = 12000; 

        private int taskType = POST_TASK; 
        private Context mContext = null; 
        private String processMessage = "Processing..."; 

        private ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(); 

        private ProgressDialog pDlg = null; 

        public checkAS(int taskType, Context mContext, String processMessage) { 

         this.taskType = taskType; 
         this.mContext = mContext; 
         this.processMessage = processMessage; 
        } 

        public void addNameValuePair(String name, String value) { 

         params.add(new BasicNameValuePair(name, value)); 
        } 
        @SuppressWarnings("deprecation") 
       private void showProgressDialog() { 

         pDlg = new ProgressDialog(mContext); 
         pDlg.setMessage(processMessage); 
         pDlg.setProgressDrawable(mContext.getWallpaper()); 
         pDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
         pDlg.setCancelable(false); 
         pDlg.show(); 

        } 

        @Override 
        protected void onPreExecute() { 

         showProgressDialog(); 

        } 

        protected String doInBackground(String... urls) { 

         String url = urls[0]; 
         String result = ""; 

         HttpResponse response = doResponse(url); 

         if (response == null) { 
          return result; 
         } else { 

          try { 

           result = inputStreamToString(response.getEntity().getContent()); 

          } catch (IllegalStateException e) { 
           Log.e(TAG, e.getLocalizedMessage(), e); 

          } catch (IOException e) { 
           Log.e(TAG, e.getLocalizedMessage(), e); 
          } 

         } 

         return result; 
        } 

        @Override 
        protected void onPostExecute(String response) { 

         handleResponse(response); 
         pDlg.dismiss(); 

        } 
private HttpParams getHttpParams() { 

      HttpParams htpp = new BasicHttpParams(); 

      HttpConnectionParams.setConnectionTimeout(htpp, CONN_TIMEOUT); 
      HttpConnectionParams.setSoTimeout(htpp, SOCKET_TIMEOUT); 

      return htpp; 
     } 

     private HttpResponse doResponse(String url) { 

      // Use our connection and data timeouts as parameters for our 
      // DefaultHttpClient 
      HttpClient httpclient = new DefaultHttpClient(getHttpParams()); 

      HttpResponse response = null; 

      try { 
       switch (taskType) { 

       case POST_TASK: 

        HttpPost httppost= new HttpPost(url); 
        httppost.setEntity(new UrlEncodedFormEntity(params)); 
        response = httpclient.execute(httppost); 

        break; 
       } 
      } 
      catch (Exception e) { 
      // display("Remote DataBase can not be connected.\nPlease check network connection."); 

       Log.e(TAG, e.getLocalizedMessage(), e); 
       return null; 

      } 

      return response; 
     } 

     private String inputStreamToString(InputStream is) { 

      String line = ""; 
      StringBuilder total = new StringBuilder(); 

      // Wrap a BufferedReader around the InputStream 
      BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 

      try { 
       // Read response until the end 
       while ((line = rd.readLine()) != null) { 
        total.append(line); 
       } 
      } catch (IOException e) { 
       Log.e(TAG, e.getLocalizedMessage(), e); 
      } 
      catch(Exception e) 
      { 
       Log.e(TAG, e.getLocalizedMessage(), e); 
      } 

      // Return full string 
      return total.toString(); 
     } 

    } 
    public void handleResponse(String response) 

    {  
    //display("Response:"+response); 
     if(!response.equalsIgnoreCase("")) 
     { 
      JSONObject jso; 
      try { 
       //do your stuff 
       } 
      catch (JSONException e1) { 

       Log.e(TAG, e1.getLocalizedMessage(), e1); 
      } 
      catch(Exception e) 
      { 
       Log.e(TAG, e.getLocalizedMessage(), e); 
      } 


     } 
     else 
     { 
      display("Could not able to reach Server!"); 
     } 


    } 
+0

哪里是handleResponse(响应)方法? – Saty

+0

@Saty现在我更新我的代码找到它 – Boopathi

+0

请参阅@Boopathi我不想为我这样冗长的代码我只是上传一些文件到服务器,因为会有一些文件上传,所以我做一个循环,并打电话给AsyncTask从它一个接一个。这是造成这个问题的原因。有没有更好的方法来做到这一点? – Saty

0

尝试这个:

private class checkAS extends AsyncTask<Void, Void, Boolean> { 
    ProgressDialog dialogue; 

    @Override 
    protected void onPostExecute(Boolean result) { 
     super.onPostExecute(result); 
     dialogue.dismiss(); 
    } 
     @Override 
     protected Boolean doInBackground(Void... params) { 
      try { 
       Thread.sleep(15000); 
      } catch (Exception e) {} 
      return true; 
     } 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      dialogue = new ProgressDialog(Main.this); 
      dialogue.setTitle("Processing"); 
      dialogue.setMessage("Getting Profile Information"); 
      dialogue.setIndeterminate(true); 
      dialogue.setCancelable(false); 
      dialogue.show(); 
     } 
    } 
相关问题