Android docking webService interface

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: password: k57x

Such as:

    WEB_SERVER_URL = "";
/ / namespace
               NAMESPACE = "";

Open in browser:


Targeanamespace = "": 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


package com.example.a_test.webservice;

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 = "";
    // Namespace
    private static final String NAMESPACE = "";
     * @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 =;
                // 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.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;

        // Handler for communication between child thread and main thread
        final Handler mHandler = new Handler() {

            public void handleMessage(Message msg) {
                switch (msg.what) {
			case 0:
			    // callBack the return value to the parameter of callBack
	                    webServiceCallBack.callBack((SoapObject) msg.obj,"");
			case 1:
			    // callBack the return value to the parameter of callBack
			    webServiceCallBack.callBack(null,(String) msg.obj);


        // Open thread to access WebService
        executorService.submit(new Runnable() {
            public void run() {
                SoapObject resultSoapObject = null;
                try {
           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
                } catch (HttpResponseException e) {
                } catch (IOException e) {
                } catch (XmlPullParserException e) {
     * Data return results
    public interface WebServiceCallBack {
        public void callBack(SoapObject result,String errorInfo);


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() {
    public void callBack(SoapObject result, String errorInfo) {
	if(result != null){
	    Log.e("----------", result.toString());
	    String str = result.getProperty(0).toString();
	} else {

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.




Keywords: Java Android

Added by vahidf on Fri, 03 Jan 2020 11:19:23 +0200