I have been trying to connect to the w3schools tempconvert web service with an andorid using ksoap2, however the result I get whenever calling a method is com.example.myproject.MyTask@
The code I have used is
public class MyTask extends AsyncTask<String, Integer, String>{
private static final String SOAP_ACTION = "http://www.w3schools.com/webservices/CelsiusToFahrenheit";
private static final String OPERATION_NAME = "CelsiusToFahrenheit";
private static final String NAMESPACE = "http://www.w3schools.com/webservices/";
private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
@Override
protected String doInBackground(String... params) {
String response = null;
SoapObject Request = new SoapObject(NAMESPACE, OPERATION_NAME);
Request.addProperty("Celsius", "1");
//Request.addProperty("strCommandParameters", params[1]);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
// Needed to make the internet call
// Allow for debugging - needed to output the request
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
// this is the actual part that will call the webservice
try {
androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
} catch (HttpResponseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject) soapEnvelope.bodyIn;
response = result.getProperty(0).toString();
return response;
}
}
and I call this from my onCreate method using
MyTask myTask = new MyTask();
myTask.execute(new String[] {"Celsius", "1"}).toString()
(Btw I realise sending the parameters to the method is pointless because they are set in the called method.)
*******MyTask Class********
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.content.Context;
import android.os.AsyncTask;
public class MyTask extends AsyncTask<String, Integer, String> {
private AsyncTaskCompleteListener callback;
public MyTask(Context context, MainActivity mainActivity) {
// TODO Auto-generated constructor stub
callback = mainActivity;
}
private static final String SOAP_ACTION = "http://www.w3schools.com/webservices/CelsiusToFahrenheit";
private static final String OPERATION_NAME = "CelsiusToFahrenheit";
private static final String NAMESPACE = "http://www.w3schools.com/webservices/";
private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
@Override
protected String doInBackground(String... params) {
String response = null;
SoapObject Request = new SoapObject(NAMESPACE, OPERATION_NAME);
Request.addProperty("Celsius", "1");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
// this is the actual part that will call the webservice
try {
androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject) soapEnvelope.bodyIn;
response = result.getProperty(0).toString();
return response;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
callback.onTaskComplete(result);
super.onPostExecute(result);
}
}
*******AsyncTaskCompleteListener******
Create a new separate Interface
public interface AsyncTaskCompleteListener {
public void onTaskComplete(String result);
}
*******MainActivity********
1. your main activity must implements AsyncTaskCompleteListener
2. override the below method in your main activity.
@Override
public void onTaskComplete(String result) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
call MyTask class using
new MyTask(getApplicationContext(), MainActivity.this).execute();