ProgressDialog挂起asynctask

时间:2013-01-24 21:19:24

标签: android android-asynctask progressdialog

(我正在使用2.3.3) 以下代码是一个异步任务,用于显示进度对话框(pd),在HttpPost中执行doInBackground(),并在onPostExecute()中关闭进度对话框。它在75%的时间内完美运行。但是,有25%的时间pd显示并且永远不会消失(但是帖子已完成)。用户摆脱pd的唯一方法是进入设置并终止应用程序(即使“cancelable设置为true”)。

没有发生超时异常。

onPostExecute()未执行或pd.dismiss()无效。

如果我取消了pd,一切都会在100%的时间内完美运行。

发生了什么事?感谢。

class postStringToURLTask extends AsyncTask<URL, Void, String> {
    String responseString = "";

    @Override
    protected void onPreExecute() {
        String myEmailAddress = getPref("emailaddress");
        pd = ProgressDialog.show(PSActivity.this, 
                                "Emailing Trip to " + myEmailAddress, "", true);
    }

    @Override
    protected String doInBackground(URL... urls) {
        try {
            URL onlyURL = urls[0];  //there is only one url
            HttpResponse response = null;
            HttpParams myParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(myParams,4000);
            HttpConnectionParams.setSoTimeout(myParams, 4000);
            HttpClient httpclient = new DefaultHttpClient(myParams);
            HttpPost myPost = new HttpPost(onlyURL.toString());
            StringEntity se = new StringEntity(payloadString);
            myPost.setEntity(se);
            myPost.setHeader("Accept", "application/json");
            myPost.setHeader("Content-type", "text/plain");
            response = httpclient.execute(myPost);
            BufferedReader rd = new BufferedReader(
                          new InputStreamReader(response.getEntity().getContent()));

            String line = "";
            while ((line = rd.readLine()) != null) {
                responseString = line;
            } 
        } catch (Exception e) {
            debugLog("postStringToURLTask got exception" + e.getMessage().toString() , 1); 
            responseString = "error  " + e.getMessage().toString();
        }        

        return responseString;
    }

    @Override
    protected void onPostExecute(String result) {
        pd.dismiss();
        debugLog("just dismissed progress dialog", 1);
        result = result.replace("[","");
        result = result.replace("\"","");
        result = result.replace("]","");

        ////log.w(getClass().getName(), "onPostExecute received: " + result);
        if (result == "") {
            mpGood.start();
        } else {
            sdTripSentProblem(); //bad news dialog
            logToServer(result);
        }  
    }
}

0 个答案:

没有答案