Using ksoap2 to call webservice in Android

Keywords: xml Java Android Web Development

In recent demand, Android app calls remote webservice and checks it on the Internet. In general, it uses ksoap2 for a try. At first, it is not successful. Then it uses axis used in web development last year for reference. Finally, it returns to ksoap2. After several attempts, it is finally successful. The share is as follows:

1, Reference

Reference 1: analysis of SoapUI: the method name is checkIn, there is a parameter, the name is "strXML", and the namespace is "http://tempuri.org".

Reference 1: refer to the successful version of C ා: C ා is relatively complete. It can bring out the method name directly without specifying the parameter name. It can be called directly without considering the namespace.

ServiceEtMobile2.sDataInfraceSoapClient client = new ServiceEtMobile2.sDataInfraceSoapClient();
string xml = makeCheckInXml();
string resp = client.checkIn(xml);
//...

2, java code

It can be seen that implementation in java is more troublesome. You need to specify namespace, method name and parameter name. In addition, this operation needs to be performed by another sub thread.

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

//...

String WSDL_URI = "http://Fs.52myb.com/ncpcs/sdatainfrace.asmx? WSDL "; / / uri of WSDL
			
String namespace = "http://tempuri.org/";//namespace
String methodName = "checkIn";//Method name to call
//Set SoapAction
String soapAction = namespace + methodName;
	        
String xml = makeCheckInXml();
	        
SoapObject request = new SoapObject(namespace, methodName);	        
request.addProperty("strXML", xml);
	      
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
envelope.bodyOut = request; //Since the request is sent, the bodyOut is set
envelope.dotNet = true; //Because it is a web service developed by. net, it should be set to true here
try{
	HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL_URI);
	httpTransportSE.call(soapAction, envelope);
}catch(Exception e){
	e.printStackTrace();
	System.out.println("checkIn report errors:"+ e.getMessage());
}
	       
SoapObject object = (SoapObject) envelope.bodyIn;
String resp = object.getProperty(0).toString();
System.out.println("checkIn Return results:"+ resp);

Successful return:

Problem solving.

 

Posted by ICEcoffee on Mon, 23 Dec 2019 09:05:28 -0800