webService interface
In this paper, web service is programmed in Java, and ksoap2-3.6.2.jar is used for Android call.
Download link of jar package: https://pan.baidu.com/s/1mxhf0i3fn86hsza7r7org password: k57x
Such as:
WEB_SERVER_URL = "http://192.168.191.1:8080/WT600WebService/wtwebservice?wsdl";
/ / namespace
NAMESPACE = "http://serviceimpl.app.wonder.com/";
Open in browser:
Explain:
Targeanamespace = "http://serviceimpl.app.wonder.com/": namespace.
name="ServiceWT600ImplService": the name of the service.
1: Method called HelloWorld with no arguments.
2: The return data of HelloWorld is: HelloWorldResponse{return=HelloWorld}. The returned data value is the data corresponding to "return".
3: The function named itcIsOnline of the method needs to pass a parameter of type String. Parameter passing is similar to the key value pair in the form of map. The name of the corresponding key is "arg0". That is to say, when calling this method, you need to use SoapObject to store the parameter in arg0.
// Create SoapObject object object SoapObject soapObject = new SoapObject(nameSpace, methodName); // SoapObject add parameter soapObject.addProperty("arg0", "Uploaded data");
4: The data returned by method itcIsOnline is in the same format as 2.
Tool class
WebServiceUtils:
package com.example.a_test.webservice; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpResponseException; import org.ksoap2.transport.HttpTransportSE; import org.xmlpull.v1.XmlPullParserException; import android.os.Handler; import android.os.Message; /** * @author ${M.xang} * Creation time: 10:00:37 am, August 25, 2018 * Class description: * WebService Tool class */ public class WebServiceUtils { // Thread pool with 3 threads private static final ExecutorService executorService = Executors.newFixedThreadPool(3); public static final String WEB_SERVER_URL = "http://192.168.191.1:8080/WT600WebService/wtwebservice?wsdl"; // Namespace private static final String NAMESPACE = "http://serviceimpl.app.wonder.com/"; /** * @param methodName * WebService Call method name of * @param properties * WebService Parameters * @param webServiceCallBack * Callback interface */ public static void callWebService(final String methodName, HashMap<String, String> properties,final WebServiceCallBack webServiceCallBack) { // Create HttpTransportSE object, pass WebService server address, and set the timeout to 30s final HttpTransportSE httpTransportSE = new HttpTransportSE(WEB_SERVER_URL, 30*1000); // Create SoapObject object object SoapObject soapObject = new SoapObject(NAMESPACE, methodName); // SoapObject adds parameters. If properties passes null, it means calling function without parameters if (properties != null) { for (Iterator<Map.Entry<String, String>> it = properties.entrySet() .iterator(); it.hasNext();) { Map.Entry<String, String> entry = it.next(); // Save the uploaded parameter data to soapObject soapObject.addProperty(entry.getKey(), entry.getValue()); } } // Instantiate SoapSerializationEnvelope and pass in the version number of the SOAP protocol of the WebService final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); soapEnvelope.bodyOut = soapObject; soapEnvelope.dotNet = false;// Set whether to call. Net developed WebService. Be sure to set it to false soapEnvelope.encodingStyle = "UTF-8"; httpTransportSE.debug = true; soapEnvelope.setOutputSoapObject(soapObject); // Handler for communication between child thread and main thread final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 0: // callBack the return value to the parameter of callBack webServiceCallBack.callBack((SoapObject) msg.obj,""); break; case 1: // callBack the return value to the parameter of callBack webServiceCallBack.callBack(null,(String) msg.obj); break; default: break; } } }; // Open thread to access WebService executorService.submit(new Runnable() { @Override public void run() { SoapObject resultSoapObject = null; try { httpTransportSE.call(NAMESPACE+ methodName, soapEnvelope); if (soapEnvelope.getResponse() != null) { // Get SoapObject returned by server response resultSoapObject = (SoapObject) soapEnvelope.bodyIn; } // Send the obtained message to the main thread with Handler mHandler.sendMessage(mHandler.obtainMessage(0,resultSoapObject)); } catch (HttpResponseException e) { e.printStackTrace(); mHandler.sendMessage(mHandler.obtainMessage(1,e.toString())); } catch (IOException e) { e.printStackTrace(); mHandler.sendMessage(mHandler.obtainMessage(1,e.toString())); } catch (XmlPullParserException e) { e.printStackTrace(); mHandler.sendMessage(mHandler.obtainMessage(1,e.toString())); } } }); } /** * Data return results */ public interface WebServiceCallBack { public void callBack(SoapObject result,String errorInfo); } }
Explain:
soapEnvelope.dotNet = false; / / set whether the. Net developed WebService is called.
If it is not. Net developed WebService, it must be set to false here, otherwise it will be particularly sour.... don't ask me why, I tested for a long time before I found the problem.
Calling method
Use in Activity:
HashMap<String,String> map = new HashMap<String,String>(); map.put("arg0", xmlPara); // Upload data WebServiceUtils.callWebService(webMethod, map, new WebServiceCallBack() { @Override public void callBack(SoapObject result, String errorInfo) { if(result != null){ Log.e("----------", result.toString()); String str = result.getProperty(0).toString(); tv_resultInfo.setText(str); } else { if(!errorInfo.equals("")){ tv_resultInfo.setText(errorInfo); } } } });
Description: arg0 is the key of the parameter corresponding to the method (arg0 is the default value, possibly other values, as shown in the figure below), and xmlpara is the data to be uploaded.