Reputation: 21003
I am trying to call an asmx web service from an Android app. Just literally started some Android development today. I've been following various solutions I have found on the net and on here and it seems it is more difficult than anticipated. I have tried various solutions and using KSoap2 seems to be the easiest way to implement this, well it would be if I could get it working.
I have the following code which works up until a point:
private class CallWebService extends AsyncTask<Void, Void, Void> {
private static final String SOAP_ACTION = "http://tempuri.org/GetUser";
private static final String METHOD_NAME = "GetUser";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://160.10.1.79:59315/Service1.asmx";
TextView tv;
@Override
protected Void doInBackground(Void... params) {
tv=(TextView)findViewById(R.id.txtMessage);
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = (Object)envelope.getResponse();
tv.setText(result.toString());
}
catch (Exception e) {
tv.setText(e.getMessage());
}
return null;
}
}
It seems to hang at the line androidHttpTransport.call(SOAP_ACTION, envelope);
Any ideas why? Is this the correct approach? Should I be looking in another direction?
Upvotes: 2
Views: 608
Reputation: 21003
Ok the solution for this was quite simple in the end. The web service was running under localhost, but would not work using localhost:59315
. So I changed it to **MY IP**:59315
. Still didn't work. Using this question Accessing localhost:port from Android emulator I tried using 10.0.2.2:59315
and it worked. So thanks go to Akhil Jain in the other question.
Upvotes: 1
Reputation: 7415
tv.setText(result.toString());
It is not at all recommended to access UI from doInBackground
.
doInBackground
will automatically pass result to onPostExecute
and you can set the text there
protected void onPostExecute(String result){
tv.setText(result.toString());
}
Upvotes: 1