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?