尝试使用HTTPPOST时,与doInBackground相关的异步错误

时间:2013-05-16 15:07:52

标签: java android eclipse android-asynctask

我是Android开发的新手,我在发送请求后尝试从服务器接收xml文件。

所以我试图连接到服务器并发送带有httppost的xml字符串,但由于某种原因,我的asyncTask出错了。我尝试没有异步任务,但新的Android设备引发异常......

private class Connection extends AsyncTask<Object, Object, Object> {

                    protected Object doInBackground(Object... arg0) {
                        connect();
                        return null;
                    }

                }
    private void connect() {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://dev.engineersnetwork.net/enetdevservices/");

        try {
            StringEntity se = new StringEntity( "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"><soap12:Body><LineTest xmlns=\"http://www.engineersnetwork.net/enetdevservices/\"></LineTest></soap12:Body></soap12:Envelope>", HTTP.UTF_8);
            se.setContentType("text/xml");
            httppost.setEntity(se);

            HttpResponse httpresponse = httpclient.execute(httppost);
            HttpEntity resEntity = httpresponse.getEntity();
            TextView tvData = (TextView)findViewById(R.id.tvData);
            tvData.setText(EntityUtils.toString(resEntity));

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

    } 

这是日志:

    05-16 15:42:32.220: D/libEGL(8865): loaded /system/lib/egl/libEGL_mali.so
05-16 15:42:32.230: D/libEGL(8865): loaded /system/lib/egl/libGLESv1_CM_mali.so
05-16 15:42:32.230: D/libEGL(8865): loaded /system/lib/egl/libGLESv2_mali.so
05-16 15:42:32.260: D/OpenGLRenderer(8865): Enabling debug mode 0
05-16 15:42:32.310: W/dalvikvm(8865): threadid=11: thread exiting with uncaught exception (group=0x412d62a0)
05-16 15:42:32.310: E/AndroidRuntime(8865): FATAL EXCEPTION: AsyncTask #1
05-16 15:42:32.310: E/AndroidRuntime(8865): java.lang.RuntimeException: An error occured while executing doInBackground()
05-16 15:42:32.310: E/AndroidRuntime(8865):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at java.lang.Thread.run(Thread.java:856)
05-16 15:42:32.310: E/AndroidRuntime(8865): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
05-16 15:42:32.310: E/AndroidRuntime(8865):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4876)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:948)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at android.view.View.requestLayout(View.java:15245)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at android.view.View.requestLayout(View.java:15245)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at android.view.View.requestLayout(View.java:15245)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at android.view.View.requestLayout(View.java:15245)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:292)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at android.view.View.requestLayout(View.java:15245)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at android.widget.TextView.checkForRelayout(TextView.java:6623)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at android.widget.TextView.setText(TextView.java:3727)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at android.widget.TextView.setText(TextView.java:3585)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at android.widget.TextView.setText(TextView.java:3560)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at vancert.test2.MainActivity.connect(MainActivity.java:52)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at vancert.test2.MainActivity.access$0(MainActivity.java:39)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at vancert.test2.MainActivity$Connection.doInBackground(MainActivity.java:34)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-16 15:42:32.310: E/AndroidRuntime(8865):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-16 15:42:32.310: E/AndroidRuntime(8865):     ... 5 more
05-16 15:42:42.260: I/Process(8865): Sending signal. PID: 8865 SIG: 9

任何提示?

2 个答案:

答案 0 :(得分:3)

 TextView tvData = (TextView)findViewById(R.id.tvData);
 tvData.setText(EntityUtils.toString(resEntity));

此代码无法在doInBackground内运行。它必须由UI Thread执行。

使用HandlerrunOnUiThread将可运行的内容发布到UI Thread邮件队列

答案 1 :(得分:0)

您收到这些错误是因为您的asynctask正在后台线程中运行,并且您正在设置主要ui线程上的textview的值。您无法使用后台线程中的主ui线程视图。     将这些代码放在asynctask的postexecute()中。

TextView tvData = (TextView)findViewById(R.id.tvData);
 tvData.setText(EntityUtils.toString(resEntity));