0
***This is my main class***

这是我的主要活动代码,它没有运行并且出现错误!我正在尝试调用webserive我制作的a Asp.net,但它给出了错误,webservice在网站上运行良好,我应该先在网站上安装 a webserivce,然后将其与 android 应用程序集成吗?请帮助我,因为我是一名业余开发人员,正在努力通过这个学习阶段。谢谢你。

package example.prgguru.com.myfirstwebservice;

            //In Here Your package com."    your one     ";

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class MainActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    private static final String SOAP_ACTION = "http://tempuri.org/GetUserDetails";

    private static final String OPERATION_NAME = "GetUserDetails";// your webservice web method name

    private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";

    private static final String SOAP_ADDRESS = "http://localhost:51925/SampleService/Service.asmx";
// for the SOAP_ADDRESS, run your web service & see
//your web service Url :1506/WebSite3/Service.asmx ,1052 will be change according to your PC

    TextView tvData1;
    EditText edata;
    Button button;
    String GDTNo;
//http://localhost:1827/WebSite1/Service.asmx/HelloWorld

    //http://10.0.2.2:1827/WebSite1/Service.asmx
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvData1 = (TextView) findViewById(R.id.textView1);


        button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                DownloadFilesTask task=new DownloadFilesTask();
                task.execute();


            }
        });


        //client = new DefaultHttpClient();
        //new Read().execute("text");
    }

    private class DownloadFilesTask extends AsyncTask<String, String, String> {


        @Override
        protected String doInBackground(String... params) {

            SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
            PropertyInfo propertyInfo = new PropertyInfo();
            propertyInfo.type = PropertyInfo.STRING_CLASS;
            propertyInfo.name = "eid";

            edata = (EditText) findViewById(R.id.editText1);
            GDTNo = edata.getText().toString();

            request.addProperty(propertyInfo, GDTNo);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;

            envelope.setOutputSoapObject(request);

            HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);

            try {
                httpTransport.call(SOAP_ACTION, envelope);
                Object response = envelope.getResponse();
                tvData1.setText(response.toString());
            } catch (Exception exception) {
                tvData1.setText(exception.toString() + "  Or enter number is not Available!");
            }

            tvData1 = (TextView) findViewById(R.id.textView1);

            return null;
        }



    }
}

LOGCAT

 03-10 02:44:21.340      788-788/example.prgguru.com.myfirstwebservice D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
03-10 02:44:22.360      788-788/example.prgguru.com.myfirstwebservice D/﹕ HostConnection::get() New Host Connection established 0xb7464cc8, tid 788
03-10 02:44:22.520      788-788/example.prgguru.com.myfirstwebservice W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-10 02:44:22.800      788-788/example.prgguru.com.myfirstwebservice D/OpenGLRenderer﹕ Enabling debug mode 0
03-10 02:45:15.710      788-788/example.prgguru.com.myfirstwebservice W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-10 02:45:22.920      788-788/example.prgguru.com.myfirstwebservice W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-10 02:45:27.340      788-788/example.prgguru.com.myfirstwebservice D/dalvikvm﹕ GC_FOR_ALLOC freed 240K, 10% free 3043K/3360K, paused 46ms, total 54ms
03-10 02:57:17.490      788-788/example.prgguru.com.myfirstwebservice W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
03-10 02:57:34.380    1116-1116/example.prgguru.com.myfirstwebservice D/﹕ HostConnection::get() New Host Connection established 0xb7464cc8, tid 1116
03-10 02:57:34.530    1116-1116/example.prgguru.com.myfirstwebservice W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-10 02:57:34.550    1116-1116/example.prgguru.com.myfirstwebservice D/OpenGLRenderer﹕ Enabling debug mode 0
03-10 02:57:36.600    1116-1116/example.prgguru.com.myfirstwebservice W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-10 02:57:43.000    1116-1130/example.prgguru.com.myfirstwebservice W/dalvikvm﹕ threadid=12: thread exiting with uncaught exception (group=0xb3a8fba8)
03-10 02:57:43.100    1116-1130/example.prgguru.com.myfirstwebservice E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
    Process: example.prgguru.com.myfirstwebservice, PID: 1116
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
            at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094)
            at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:824)
            at android.view.View.requestLayout(View.java:16431)
            at android.view.View.requestLayout(View.java:16431)
            at android.view.View.requestLayout(View.java:16431)
            at android.view.View.requestLayout(View.java:16431)
            at android.view.View.requestLayout(View.java:16431)
            at android.widget.TextView.checkForRelayout(TextView.java:6600)
            at android.widget.TextView.setText(TextView.java:3813)
            at android.widget.TextView.setText(TextView.java:3671)
            at android.widget.TextView.setText(TextView.java:3646)
            at example.prgguru.com.myfirstwebservice.MainActivity$DownloadFilesTask.doInBackground(MainActivity.java:96)
            at example.prgguru.com.myfirstwebservice.MainActivity$DownloadFilesTask.doInBackground(MainActivity.java:67)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
03-10 02:57:43.170    1116-1130/example.prgguru.com.myfirstwebservice D/dalvikvm﹕ GC_FOR_ALLOC freed 235K, 10% free 3048K/3360K, paused 44ms, total 44ms
03-10 02:57:46.140    1116-1130/example.prgguru.com.myfirstwebservice I/Process﹕ Sending signal. PID: 1116 SIG: 9
4

2 回答 2

1

As the exception message told you, you can not change UI in non-UI thread.

AsyncTask.doInBackground is executed in another thread. If you try to update ui, you need to put those code in AsyncTask.onPostExecute.

于 2015-03-10T08:25:46.997 回答
0

你不能findViewById(R.id.editText1);在 inside使用doInBackground,因为默认View是由主 UI 线程创建的,但doInBackground在后台线程中。然后解决方案是您可以GDTNo作为String参数传递给AsyncTask.

希望这有帮助!

于 2015-03-10T08:58:44.053 回答