soap协议网络连接这算是一个demo,总的来说比较好用,但是有bug,在没有网络的情况下闪退报错,所以用的时候必须要添加判断。
package com.ladongjiguang.meikuangwuziyunshu.PublicClass;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.os.Handler;
import android.os.Message;
public class WebServiceUtils {
public static final String WEB_SERVER_URL = "http://192.168.2.220:81/Index.asmx";
// 含有3个线程的线程池
private static final ExecutorService executorService = Executors
.newFixedThreadPool(3);
// 命名空间
private static final String NAMESPACE = "http://tempuri.org/";
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开发的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) {
resultSoapObject = (SoapObject) soapEnvelope.bodyIn;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
mHandler.sendMessage(mHandler.obtainMessage(0,
resultSoapObject));
}
}
});
}
public interface WebServiceCallBack {
public void callBack(SoapObject result);
}
}
用法:
//添加参数
HashMap<String, String> properties = new HashMap<String, String>();
properties.put("Xml", "<Request><data code='select-Secretkey'><no><Account>"user_id.getText()+"</Account></no></data></Request>");
properties.put("Token", "");
//通过工具类调用WebService接口
WebServiceUtils.callWebService(WebServiceUtils.WEB_SERVER_URL, "DynamicInvoke", properties, new WebServiceUtils.WebServiceCallBack() {
//WebService接口返回的数据回调到这个方法中
@Override
public void callBack(SoapObject result) {
if(result != null){
Object detail = result.getProperty("DynamicInvokeResult");
JSONObject jsonObj = null;
try {
jsonObj = XML.toJSONObject(detail.toString());//xml转变成json,需要用到jar包,这个方法很好用,记得保留
} catch (JSONException e) {
Log.e("JSON exception", e.getMessage());
e.printStackTrace();
}
}
}
});
上面例子是一个用用户id获取秘钥的方法