0

Ok I have searched StackOverflow for this answer along with the web and I have had no luck... I'm just trying to do the most basic web service function...HelloWorld I have a button that you click and when clicked it makes the text set to whatever the function in the webservice returns.. Here is my code:

public class MainActivity extends ActionBarActivity {

private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://myInfo/App/Service1.asmx";
private final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
private final String METHOD_NAME = "HelloWorld";
private  String TAG = "SOAP";

private static String message;

Button b;
TextView tv;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


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

     b = (Button) findViewById(R.id.button1);
        //Button Click Listener
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {


                    //Create instance for AsyncCallWS
                    AsyncCallWS task = new AsyncCallWS();
                    //Call execute 
                    task.execute();
                    //If text control is empty

            }
        });
    }


private class AsyncCallWS extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... params) {
        Log.i(TAG, "doInBackground");
        getMessage();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        Log.i(TAG, "onPostExecute");
        tv.setText(message);
    }

    @Override
    protected void onPreExecute() {
        Log.i(TAG, "onPreExecute");
        tv.setText("Rendering...");
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        Log.i(TAG, "onProgressUpdate");
    }

}

public void getMessage() {

    //Create request
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    //Property which holds input parameters
    /*PropertyInfo messagePI = new PropertyInfo();
    //Set Name
    messagePI.setName("Practice");
    //Set Value
    messagePI.setValue(messagePI);
    //Set dataType
    messagePI.setType(double.class);
    //Add the property to request object
    request.addProperty(messagePI);*/
    //Create envelope
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.dotNet = true;
    //Set output SOAP object
    envelope.setOutputSoapObject(request);
    //Create HTTP call object
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        //Invoke web service
        Log.i(TAG, "Point A");
        androidHttpTransport.debug=true;
        androidHttpTransport.call(SOAP_ACTION, envelope);
        //androidHttpTransport.responseDump;
        //Get the response
        //SoapObject result = (SoapObject) envelope.bodyIn;
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        //Assign it to fahren static variable
        message = response.toString();

        /*if(message !=null){
            tv.setText(meesage.getProperty(0).toString());
        }*/


        //tv.setText("" + results);


    } catch (Exception e) {
        tv.setText(e.getMessage());
        e.printStackTrace();
    }
}

Here is my LogCat

07-21 14:07:50.519: E/AndroidRuntime(9000): FATAL EXCEPTION: AsyncTask #1
07-21 14:07:50.519: E/AndroidRuntime(9000): Process: com.example.mysoap, PID: 9000
07-21 14:07:50.519: E/AndroidRuntime(9000): java.lang.RuntimeException: An error occured while executing doInBackground()
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at java.lang.Thread.run(Thread.java:841)
07-21 14:07:50.519: E/AndroidRuntime(9000): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6309)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:861)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.view.View.requestLayout(View.java:16737)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.view.View.requestLayout(View.java:16737)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.view.View.requestLayout(View.java:16737)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.view.View.requestLayout(View.java:16737)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:352)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.view.View.requestLayout(View.java:16737)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.widget.TextView.checkForRelayout(TextView.java:6724)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.widget.TextView.setText(TextView.java:3911)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.widget.TextView.setText(TextView.java:3769)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.widget.TextView.setText(TextView.java:3744)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at com.example.mysoap.MainActivity.getMessage(MainActivity.java:148)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at com.example.mysoap.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:81)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at com.example.mysoap.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:1)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-21 14:07:50.519: E/AndroidRuntime(9000):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-21 14:07:50.519: E/AndroidRuntime(9000):     ... 4 more

When I just run the program the LogCat error shows up but when I actually step through the program I get this message

org.xmlpull.v1.XmlPullParserException: expected: START_TAG

at this line: } catch (Exception e) {

Any help would be greatly appreciated... I've been stuck on this for awhile now sadly... :(

bbesase
  • 791
  • 4
  • 18
  • 31

2 Answers2

1

I think the issue is here: Since now you are in the background thread(doInBackground()) and trying to set the text(tv.setText). Only the main thread can do it. Remove that statement "tv.setText(e.getMessage());" It should work

catch (Exception e) {
        tv.setText(e.getMessage());
        e.printStackTrace();
    }

This will fix the issue: Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

Then you can try to fix the other issue.

Check whether the below link helps you for the other fix: ksoap2 org.xmlpull.v1.xmlpullparserexception expected start_tag error

Community
  • 1
  • 1
Sweety Bertilla
  • 972
  • 10
  • 35
  • So You're saying remove `catch (Exception e) { tv.setText(e.getMessage()); e.printStackTrace(); }`? – bbesase Jul 21 '14 at 19:09
  • 1
    no just remove the tv.setText(e.getMessage()); since you are in background thread and trying to update the UI – Sweety Bertilla Jul 21 '14 at 19:10
  • Ahh gotcha! You are right it no longer crashes out, but it still doesnt return the info I want... checking the link you sent now – bbesase Jul 21 '14 at 19:12
  • After following the link I still am not able to get the data from the web service... :( – bbesase Jul 21 '14 at 19:30
1

Yes, you can not call setText() in background. Just return the value from getMessage() method and return that same value in doInBackground(). You will get that value in onPostExecute() where you can set it by calling setText().

TechHelper
  • 822
  • 10
  • 24