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

This is my main activity code, it's not running and is giving errors! I am trying to call a webserive that I made in Asp.net, But it is giving errors the webservice is working fine on website, should I first install a webserivce on a website and then integrate it with the android app? Please help me out as I am an amatuer developer, trying to fight my way through this learning phase. thank you.

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
Asieh hojatoleslami
  • 3,240
  • 7
  • 31
  • 45

2 Answers2

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.

Qiang Jin
  • 4,427
  • 19
  • 16
0

You can not use findViewById(R.id.editText1); inside doInBackground, because default View is created by main UI thread but doInBackground is in background thread. Then solution is that you can pass GDTNo as String parameter to AsyncTask.

Hope this help!

Xcihnegn
  • 11,579
  • 10
  • 33
  • 33