1

I'm trying to run a simple example of an Android App consuming a web service. Following some examples at the web I finally did this:

private String getValueFromWS()
{
    try
    {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        String weight = "3700";
        String fromUnit = "Grams";
        String toUnit = "Kilograms";

        PropertyInfo weightProp =new PropertyInfo();
        weightProp.setName("Weight");
        weightProp.setValue(weight);
        weightProp.setType(double.class);
        request.addProperty(weightProp);

        PropertyInfo fromProp =new PropertyInfo();
        fromProp.setName("FromUnit");
        fromProp.setValue(fromUnit);
        fromProp.setType(String.class);
        request.addProperty(fromProp);

        PropertyInfo toProp =new PropertyInfo();
        toProp.setName("ToUnit");
        toProp.setValue(toUnit);
        toProp.setType(String.class);
        request.addProperty(toProp);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapPrimitive response = (SoapPrimitive)envelope.getResponse();

        return response.toString();
    }
    catch (Exception e)
    {
        return "Error: " + e.getMessage();
    }
}

I also added the

<uses-permission android:name="android.permission.INTERNET"/>

in the Manifest file.

I don't know why it throws an exception at

androidHttpTransport.call(SOAP_ACTION, envelope);

when it runs whith the emulator. I'm using 2.5.8 version of kSoap and the emulator is running Android 4.1 (level 16).

What I am doing wrong?

nachovall
  • 485
  • 1
  • 5
  • 18
  • Please provide the exception that you are seeing. Also have a look at this link http://stackoverflow.com/questions/9355707/how-to-create-soap-request-via-ksoap2 as it might be helpful to you. – BrianPlummer Jul 16 '12 at 17:38
  • It is a NetWorkOnMainThreadException. Detailed Message = null. Not much info, sorry. – nachovall Jul 17 '12 at 18:00

1 Answers1

2

Sorry for the late reply, that exception is occurring because you are executing a network call on the main UI thread. Try executing the call in an AsyncTask's doInBackground() method to offload the call from the main thread. Another, way to get around this is to change the strict mode that is probably enabled by default on the JellyBean (4.x) emulator. You can also try your code on an older version of the sdk/emulator (2.3.x) just to get it working.

This explains the error:

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

BrianPlummer
  • 164
  • 1
  • 5
  • Perfect. Now it works. Additionally, it is a good practice to do not use the main thread for WS calls. Makes the interaction with the user more friednly. Thanks a lot. – nachovall Jul 27 '12 at 10:07