Reputation: 485
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?
Upvotes: 1
Views: 770
Reputation: 164
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
Upvotes: 2