0

I have a simple java web service deployed on Tomcat7 server. I have used Axis2 as soap engine.(Web service & WSDL is OK) Here is the code I have used for the web service

  public class TestWs {
  public String sayHello(){
         return "Hello Grant";
    }
   }

I have accessed this web service from Android 2.2.It is perfectly OK.But this program not works for Android 4.0.3 Code of my Android program

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

import android.widget.TextView;
import android.app.Activity;
import android.os.Bundle;

public class AndroidWSClientActivity extends Activity {

private static final String SOAP_ACTION = "http://ws.android4.com/";
private static final String METHOD_NAME = "sayHello";
private static final String NAMESPACE = "http://ws.android4.com/sayHello/";
private static final String URL = "http://175.157.45.91:8085/ForAndroid4/services/TestWs?wsdl";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);          

    SoapSerializationEnvelope envelope = new 
    SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.setOutputSoapObject(request);

    HttpTransportSE ht = new HttpTransportSE(URL);
    try {
        ht.call(SOAP_ACTION, envelope);
        SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
        String str = response.toString();
        TextView result;
        result = (TextView)findViewById(R.id.textView1);
        result.setText(str);

    } catch (Exception e) {
        e.printStackTrace();
        TextView result;
        result = (TextView)findViewById(R.id.textView1);
        result.setText("Error!");

    }
  }
 }

Program correctly installed on Emulator. But emulator shows the message set from catch block. What is the error ??? How can I correct it ?? Thanks!

Grant
  • 4,413
  • 18
  • 56
  • 82

2 Answers2

1

In Honeycomb and Ice Cream Sandwich (i.e. Android 3.0+) , you cannot connect to the internet in the main thread (onCreate(), onPause(), onResume() etc.), and you have to instead start a new thread. The reason why this has changed is because network operations can make the app wait for a long time, and if you're running them in the main thread, the whole application becomes unresponsive. If you try to connect from the main thread, Android will throw a NetworkOnMainThreadException.

5hssba
  • 8,079
  • 2
  • 33
  • 35
  • Thnks for your answer... Can you please explain how this code should change. I'm new to Android.. – Grant Apr 07 '12 at 04:55
  • 1
    [See this link](http://stackoverflow.com/questions/9050838/how-do-you-read-text-files-off-the-internet-in-android-4-0-3) – 5hssba Apr 07 '12 at 04:57
  • You are getting a NetworkOnMainThreadException. exception right? Or any other exception? – 5hssba Apr 07 '12 at 06:06
0

In latest android version, it is not possible make web service call from UI thread.There is a similar thread related to this.

Android kSOAP web service problem

May be this will help you.

Community
  • 1
  • 1
UVM
  • 9,776
  • 6
  • 41
  • 66