httpconnection的异步任务实现中的问题

时间:2011-04-13 09:09:45

标签: android http android-asynctask

我正在努力将表单发布到google服务器并以html字符串的形式获取响应,最后我将该字符串放在webview上以显示结果....我使用异步任务,它显示的进度对话框但是有时它会向我显示“强制关闭”的消息,而不会对代码进行任何更改....这意味着对输出的预测是意外的......我的代码就是这样......

public class Urlasync extends Activity {  
   WebView engine=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
        new AddStringTask().execute();
    }

   class AddStringTask extends AsyncTask<Void, Void, HttpResponse>
    {
        HttpResponse end = null;
        String endResult = null;
        public static  final int TIMEOUT_MS=10000;
         HttpClient client=null;
         HttpPost post =null;
         List<NameValuePair> pairs=null;
         BasicResponseHandler myHandler=null;
         private final ProgressDialog dialog = new ProgressDialog(Urlasync.this);
        @Override
        protected void onPreExecute() {
             client = new DefaultHttpClient();
             HttpConnectionParams.setConnectionTimeout(client.getParams(), TIMEOUT_MS);
             HttpConnectionParams.setSoTimeout(client.getParams(), TIMEOUT_MS);
             post = new HttpPost("http://www.google.com/m");
             pairs = new ArrayList<NameValuePair>();
             pairs.add(new BasicNameValuePair("hl", "en"));
             pairs.add(new BasicNameValuePair("gl", "us"));
             pairs.add(new BasicNameValuePair("source", "android-launcher-widget"));
             pairs.add(new BasicNameValuePair("q", "persistent"));
             try {
                    post.setEntity(new UrlEncodedFormEntity(pairs));
                    SystemClock.sleep(400);
        } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
             this.dialog.setMessage("starts...");
             this.dialog.show();

          }
        @Override
        protected HttpResponse doInBackground(Void... arg0) {

            try {
            HttpResponse response = client.execute(post);
                    if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
         {
             return response;
         }
             end = response;
            } catch (ClientProtocolException e) {
                        e.printStackTrace();
            } catch (IOException e) {   //this exception is called
                e.printStackTrace();
            }
        return end;

        }
        @Override       
        protected void onPostExecute(HttpResponse params) {
             if (this.dialog.isShowing()) {
                            this.dialog.dismiss();
                             }
                    if(params!=null)
         {
            String endResult=null;
            BasicResponseHandler myHandler = new BasicResponseHandler();
                try {
                    endResult = myHandler.handleResponse(params);

                } catch (HttpResponseException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
              engine = (WebView)findViewById(R.id.webview);
          engine.loadDataWithBaseURL("http://", endResult, "text/html", "UTF-8", null);
                setContentView(R.layout.main);
                 engine.requestFocus(View.FOCUS_DOWN);
                      }
             Toast.makeText(Urlasync.this, "Done!", Toast.LENGTH_SHORT).show();
        }
    }

    }

欢迎使用代码提出建议 谢谢..

1 个答案:

答案 0 :(得分:3)

您不得在doInBackground中对用户界面执行任何操作(例如显示Toast)。请改为onPostExecuteonProgressUpdate