Async Task给出错误,用于访问Web服务?

时间:2015-03-10 07:14:43

标签: android web-services android-asynctask mssql-jdbc

***This is my main class***

这是我的主要活动代码,它没有运行并且出错! 我正在尝试拨打我在webserive中发出的Asp.net,但如果我首先在网站上安装了webservice,那么webserivce在网站上正常运行会出错网站,然后将其与Android应用程序集成?请帮助我,因为我是一名amatuer开发人员,试图在这个学习阶段努力奋斗。谢谢。

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

2 个答案:

答案 0 :(得分:1)

正如异常消息告诉您的那样,您无法在非UI线程中更改UI。

AsyncTask.doInBackground在另一个线程中执行。如果您尝试更新ui,则需要将这些代码放在AsyncTask.onPostExecute中。

答案 1 :(得分:0)

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

希望这有帮助!