android请求WebService接口,封装好的方法直接可以用

前言:
轻量数据交互常用的json和xml.对应的通信协议分别是http和soap协议
xml 就是我们今天的主角,如果后台是java php写json很快捷,如果后台是.net那发布WebService比较多,因为WebService可以跨平台。
使用步骤:
一,android 开发工具导入jar:

android请求WebService接口,封装好的方法直接可以用_第1张图片

链接:https://pan.baidu.com/s/1TYrffUO5pOeo50ErlpU2fw 密码:cyet
ksoap2-android-assembly-3.1.0-jar-with-dependencies.jar
二、封装好的class类,有注释:

   import android.os.Handler;
  import android.os.Message;
  import android.util.Log;
  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 java.io.IOException;
  import java.util.Iterator;
  java.util.Map;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
 /**
 * Created by Administrator on 2017/9/15 0015.
*
 */
 public class WebServiceUtils {
   // 含有3个线程的线程池
  private static final ExecutorService executorService = Executors
        .newFixedThreadPool(3);
// 命名空间,默认是这个,可以更改,具体需要和后台人员确认
private static final String NAMESPACE = "http://tempuri.org/";
/**
 *
 * @param url
 *            WebService服务器地址
 * @param methodName
 *            WebService的调用方法名
 * @param properties
 *            WebService的参数
 * @param webServiceCallBack
 *            回调接口
 */
public static void callWebService(String url, final String methodName,
                                  Map properties,
                                  final WebServiceCallBack webServiceCallBack) {
    // 创建HttpTransportSE对象,传递WebService服务器地址
    final HttpTransportSE httpTransportSE = new HttpTransportSE(url);
    // 创建SoapObject对象
    SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
    // SoapObject添加参数
    if (properties != null) {
        for (Iterator> it = properties.entrySet()
                .iterator(); it.hasNext();) {
            Map.Entry entry = it.next();
            soapObject.addProperty(entry.getKey(), entry.getValue());
        }
    }
    // 实例化SoapSerializationEnvelope,传入WebService的SOAP协议的版本号
    final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER12);
    // 设置是否调用的是.Net开发的WebService
    soapEnvelope.setOutputSoapObject(soapObject);
    soapEnvelope.dotNet = true;
    httpTransportSE.debug = true;
    // 用于子线程与主线程通信的Handler
    final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            // 将返回值回调到callBack的参数中
            webServiceCallBack.callBack((SoapObject) msg.obj);
        }
    };
    // 开启线程去访问WebService
    executorService.submit(new Runnable() {
        @Override
        public void run() {
            SoapObject resultSoapObject = null;
            try {
                httpTransportSE.call(NAMESPACE + methodName, soapEnvelope);
                if (soapEnvelope.getResponse() != null) {
                    // 获取服务器响应返回的SoapObject
                    Log.i("获取服务器返回的表示",soapEnvelope.getResponse()+"");//测试是                  成功返回true
                    resultSoapObject = (SoapObject) soapEnvelope.bodyIn;
                }
            } catch (HttpResponseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } finally {
                // 将获取的消息利用Handler发送到主线程
                mHandler.sendMessage(mHandler.obtainMessage(0,
                        resultSoapObject));
            }
        }
    });
}

public interface WebServiceCallBack {
    public void callBack(SoapObject result);
}
 }

三,使用

使用说明,webservice接口图解:

android请求WebService接口,封装好的方法直接可以用_第2张图片



 String methodname = "login";//登陆接口方法
Map<String, String> map = new HashMap<>();
map.put("username", "qcapp");
map.put("password", "0000");
 String url="";//接口地址对应的就是上图的地址栏url
 WebServiceUtils.callWebService(url, methodname,
      map, new WebServiceUtils.WebServiceCallBack() {
          @Override
        public void callBack(SoapObject result) {
          Log.i("请求数据",result.getProperty(0));//这里就是接口返回的数据,
        //后台可以自定义返回数据格式,这里返回json字符串,就可以正常解析了。

 }
});

你可能感兴趣的:(android请求WebService接口,封装好的方法直接可以用)