java - How to use ksoap2 to call a web service -
i have been trying connect w3schools tempconvert web service andorid using ksoap2, result whenever calling method com.example.myproject.mytask@
the code 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 make internet call // allow debugging - needed output request httptransportse androidhttptransport = new httptransportse(url); androidhttptransport.debug = true; // actual part call 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(); } // soapresult envelope body. soapobject result = (soapobject) soapenvelope.bodyin; response = result.getproperty(0).tostring(); return response; }
}
and call oncreate method using
mytask mytask = new mytask(); mytask.execute(new string[] {"celsius", "1"}).tostring()
(btw realise sending parameters method pointless because set in 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; // actual part call webservice try { androidhttptransport.call(soap_action, soapenvelope); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } // soapresult 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 new separate interface
public interface asynctaskcompletelistener { public void ontaskcomplete(string result); }
*******mainactivity********
1. main activity must implements asynctaskcompletelistener
2. override below method in 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();
Comments
Post a Comment