本文主要是介绍Android开发之访问WebService的工具类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Android中使用WebService通讯,下面对访问WebService的工具类进行记录
package com.wjy.project.railway.tools;import android.os.Handler;
import android.os.Message;import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;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;/*** Created by WangJinyong on 2018/3/28.* 访问WebService的工具类*/public class WebServiceUtils {//服务器地址public static final String WEB_SERVER_URL = "http://114.115.205.183:9004/Compaction/";
// public static final String WEB_SERVER_URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";// 命名空间private static final String NAMESPACE = "http://WebXml.com.cn/";// 含有3个线程的线程池private static final ExecutorService executorService = Executors.newFixedThreadPool(3);/*** @param url WebService服务器地址* @param methodName WebService的调用方法名* @param properties WebService的参数* @param webServiceCallBack 回调接口*/public static void callWebService(String url, final String methodName,HashMap<String, String> 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<Map.Entry<String, String>> it = properties.entrySet().iterator(); it.hasNext(); ) {Map.Entry<String, String> entry = it.next();soapObject.addProperty(entry.getKey(), entry.getValue());}}// 实例化SoapSerializationEnvelope,传入WebService的SOAP协议的版本号final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);// 设置是否调用的是.Net开发的WebServicesoapEnvelope.setOutputSoapObject(soapObject);soapEnvelope.dotNet = true;httpTransportSE.debug = true;// 用于子线程与主线程通信的Handlerfinal Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);// 将返回值回调到callBack的参数中webServiceCallBack.callBack((SoapObject) msg.obj);}};// 开启线程去访问WebServiceexecutorService.submit(new Runnable() {@Overridepublic void run() {SoapObject resultSoapObject = null;try {httpTransportSE.call(NAMESPACE + methodName, soapEnvelope);if (soapEnvelope.getResponse() != null) {// 获取服务器响应返回的SoapObjectresultSoapObject = (SoapObject) soapEnvelope.bodyIn;}} catch (IOException e) {e.printStackTrace();} catch (XmlPullParserException e) {e.printStackTrace();} finally {// 将获取的消息利用Handler发送到主线程mHandler.sendMessage(mHandler.obtainMessage(0,resultSoapObject));}}});}/*** @author WangJinyong*/public interface WebServiceCallBack {public void callBack(SoapObject result);}}
在Activity类中使用WebService对服务器进行访问
private void requestZT_G_user() {//添加参数HashMap<String, String> properties = new HashMap<String, String>();properties.put("sjh", "13801234567");properties.put("mac", hostmac);WebServiceUtils.callWebService("服务器地址", "接口的方法名", properties, new WebServiceUtils.WebServiceCallBack() {@Overridepublic void callBack(SoapObject result) {Log.e("tag","result="+result);//返回的数据}});}
这篇关于Android开发之访问WebService的工具类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!