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獲取祕鑰的方法