0

我是 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

有什么提示吗?

4

2 回答 2

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

这段代码不能在里面运行doInBackground。它必须由UI Thread.

使用HandlerorrunOnUiThread将 runnable 发布到UI Thread消息队列

于 2013-05-16T15:13:23.197 回答
0

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

TextView tvData = (TextView)findViewById(R.id.tvData);
 tvData.setText(EntityUtils.toString(resEntity));
于 2013-05-16T16:06:24.303 回答