Web service network request in Android

There are many articles about using web service in Android, here is just a brief introduction.

Here you need to first introduce the package of webservice (click download: Ksoap2.jar ), why soap? It's a long story. SOAP Protocol directly asks Du Niang.

The next step is to use:

First, there should be a url like this (http://ip: port / project name / WS (default is WS) / srvmobile (interface address)? wsdl). If not, ask the background for it.

This address can be opened on the web page.

For example:

Make do with it.

There is a lot of information in this page, including the required namespace, method name and required parameters for webservice request.

Once you get the information, you can call webservice in the project to request the background data.

The method is:

 SoapObject request = new SoapObject(namespace, methodName);
        // Set the two parameters mobileCode and userId that need to be passed in to call the WebService interface
        request.addProperty("mobileCode", phoneSec);
        request.addProperty("userId", "");

        //Create the SoapSerializationEnvelope object and specify the soap version number (as seen in the wsdl earlier)
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER12);
        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

        //The WSDL URI that you can open on the web page
        HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL_URI);
        httpTransportSE.call(null, envelope);//call

        // Get the returned data
        SoapObject object = (SoapObject) envelope.bodyIn;
        // Get returned results
        result = object.getProperty(0).toString();
        Log.d("debug",result);

This network request can be encapsulated as follows:

Here's how it works: by the way, the constants.url'name'space needs to be changed to your own.

public class WebServiceUtils {
//Change constants.url'name'space; to your own namespace
    private  final String nameSpace = Constants.URL_NAME_SPACE;
    private SoapObject msg;
    private String url;
    private String methodName;
    private OnCallListener listener;

    /**
     * Construction method
     *
     * @param url        Service address
     * @param methodName Method name
     */
    public WebServiceUtils(String url, String methodName) {
        this.url = url;
        this.methodName = methodName;
        msg = new SoapObject(nameSpace, this.methodName);

    }

    /**
     * Get the monitor
     *
     * @return
     */
    public OnCallListener getListener() {
        return listener;
    }

    /**
     * Set up a listener
     *
     * @param listener Monitor
     */
    public void setListener(OnCallListener listener) {
        this.listener = listener;
    }

    /**
     * Execution request
     */
    public void execute(OnCallListener listener) {
        this.listener = listener;
        new Work().execute();
    }

    /**
     * Request implementation
     *
     * @return
     */
    private String call() {
        SoapObject result = null;
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.bodyOut = msg;
        envelope.dotNet = false;
        HttpTransportSE sendRequest = new HttpTransportSE(url, 10 * 1000);
        envelope.setOutputSoapObject(msg);
        try {
            sendRequest.call(nameSpace + methodName, envelope);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.i("aaa", "webservice error=" + e.getMessage());
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            Log.i("aaa", "webservice error=" + e.getMessage());

        }
        try {
            result = (SoapObject) envelope.bodyIn;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            //Utils.log("webservice error =" + e.getMessage());
        }
        if (result == null) {
            return "";
        } else {
            return result.getProperty(0).toString();
        }

    }

    /**
     * Add parameter
     *
     * @param key   key
     * @param value value
     */
    public void putParam(String key, String value) {
        msg.addProperty(key, value);
    }

    /**
     * Transfer of special parameters
     *
     * @param json Parameter json
     */
    public void putParam(JSONObject json) {
        msg.addProperty("strJson", json.toString());
    }

    /**
     * Asynchronous task thread
     *
     * @author linlei
     */
    class Work extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub
            String resut = call();
            return resut;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            if (listener != null) {
                if (result.equals("")) {
                    listener.onPostFail();
                } else {
                    listener.onPostExecute(result);
                }
            }

        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            if (listener != null) {
                listener.onPreExecute();
            }
        }

    }

    /**
     * Callback interface
     */
    public interface OnCallListener {
        /**
         * Prepare to execute request
         */
        void onPreExecute();

        /**
         * Request execution complete
         *
         * @param result Return result
         */
        void onPostExecute(String result);

        /**
         * request was aborted
         */
        void onPostFail();
    }
}

This web service can be used in your project.

The above WebserviceUtils are used as follows:

  WebServiceUtils webServiceUtils = new WebServiceUtils(WSDL_URL, Method name);         
            String requesttime = String.valueOf(new Date().getTime());
//Add parameter
            webServiceUtils.putParam("requesttime", requesttime);
            webServiceUtils.execute(new WebServiceUtils.OnCallListener() {}

OK, have a good time!

Keywords: JSON Android network

Added by jc94062 on Fri, 31 Jan 2020 06:19:55 +0200