Android network request using webservice

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/u014619545/article/details/81263053

About Android use webservice many articles, here is a brief look.

Here you first introduced webservice package (Download: Ksoap2.jar ), Why is soap? Long story, soap protocol directly ask of your mother.

The next step is the use of:

The first to have such a url (http: // ip:? Port / project name / ws (the default is ws) / srvmobile (address of the interface) wsdl), if not to be asked back.

This address can be opened in a web page.

For example like this:

 Make do and see.

Many of this page information, including a request necessary for webservice namespace, the method name and parameters needed.

After obtaining this information, you can call webservice in the project to request the data background.

Method:

 SoapObject request = new SoapObject(namespace, methodName);
        // 设置需调用WebService接口需要传入的两个参数mobileCode、userId
        request.addProperty("mobileCode", phoneSec);
        request.addProperty("userId", "");

        //创建SoapSerializationEnvelope 对象,同时指定soap版本号(之前在wsdl中看到的)
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER12);
        envelope.bodyOut = request;//由于是发送请求,所以是设置bodyOut
        envelope.dotNet = true;//由于是.net开发的webservice,所以这里要设置为true

        //你的在网页上可以的打开的WSDL_URI
        HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL_URI);
        httpTransportSE.call(null, envelope);//调用

        // 获取返回的数据
        SoapObject object = (SoapObject) envelope.bodyIn;
        // 获取返回的结果
        result = object.getProperty(0).toString();
        Log.d("debug",result);

The network can request the package at:

Below, you can go back and stick with: Yes, that Constants.URL_NAME_SPACE to change your own.

public class WebServiceUtils {
//Constants.URL_NAME_SPACE; 改成你自己的namespace
    private  final String nameSpace = Constants.URL_NAME_SPACE;
    private SoapObject msg;
    private String url;
    private String methodName;
    private OnCallListener listener;

    /**
     * 构造方法
     *
     * @param url        服务地址
     * @param methodName 方法名
     */
    public WebServiceUtils(String url, String methodName) {
        this.url = url;
        this.methodName = methodName;
        msg = new SoapObject(nameSpace, this.methodName);

    }

    /**
     * 得到监听器
     *
     * @return
     */
    public OnCallListener getListener() {
        return listener;
    }

    /**
     * 设置监听器
     *
     * @param listener 监听器
     */
    public void setListener(OnCallListener listener) {
        this.listener = listener;
    }

    /**
     * 执行请求
     */
    public void execute(OnCallListener listener) {
        this.listener = listener;
        new Work().execute();
    }

    /**
     * 请求实现
     *
     * @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错误=" + e.getMessage());
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            Log.i("aaa", "webservice错误=" + e.getMessage());

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

    }

    /**
     * 添加参数
     *
     * @param key   键
     * @param value 值
     */
    public void putParam(String key, String value) {
        msg.addProperty(key, value);
    }

    /**
     * 特殊参数传值
     *
     * @param json 参数json
     */
    public void putParam(JSONObject json) {
        msg.addProperty("strJson", json.toString());
    }

    /**
     * 异步任务线程
     *
     * @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();
            }
        }

    }

    /**
     * 回调接口
     */
    public interface OnCallListener {
        /**
         * 准备执行请求
         */
        void onPreExecute();

        /**
         * 请求执行完成
         *
         * @param result 返回结果
         */
        void onPostExecute(String result);

        /**
         * 请求失败
         */
        void onPostFail();
    }
}

This webservice you can use in your project.

The above WebserviceUtils used as follows:

  WebServiceUtils webServiceUtils = new WebServiceUtils(WSDL_URL, 方法名);         
            String requesttime = String.valueOf(new Date().getTime());
//添加参数
            webServiceUtils.putParam("requesttime", requesttime);
            webServiceUtils.execute(new WebServiceUtils.OnCallListener() {}

Can, use happy!

Guess you like

Origin blog.csdn.net/u014619545/article/details/81263053