老婆保佑原环,代碼無BUG
前言
現(xiàn)在做android 的小伙伴可能網(wǎng)絡(luò)請求用的都是OKHttp,Retrofit,什么的
數(shù)據(jù)解析都是JSON,但是我就遇到了瘫絮,WebService,雖然是有點(diǎn)過時的技術(shù)填硕,但是誰叫我遇到了呢,這里記個筆記麦萤,現(xiàn)在公司也不用這種方式了,哈哈廷支,開心
目錄
- 一:引入JAR
- 下載地址
- 二:封裝WebService
- 三:再次封裝
一:引入JAR
這里我給個地址频鉴,點(diǎn)擊,進(jìn)入下載界面
二:封裝WebService
package com.allens.lib_webservice;
import android.support.v4.util.SimpleArrayMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
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;
/**
* Created by Allens on 2016/9/22.
*/
public class WebServiceUtils {
// 訪問的服務(wù)器是否由dotNet開發(fā)
public boolean isDotNet = false;
// 線程池的大小
private int threadSize = 5;
// 創(chuàng)建一個可重用固定線程數(shù)的線程池恋拍,以共享的無界隊(duì)列方式來運(yùn)行這些線程
private ExecutorService threadPool = Executors.newFixedThreadPool(threadSize);
// 連接響應(yīng)標(biāo)示
public static final int SUCCESS_FLAG = 0;
public static final int ERROR_FLAG = 1;
private WebServiceUtils() {
}
private static WebServiceUtils webServiceUtils;
public static WebServiceUtils create() {
if (webServiceUtils == null) {
synchronized (WebServiceUtils.class) {
if (webServiceUtils == null) {
webServiceUtils = new WebServiceUtils();
}
}
}
return webServiceUtils;
}
/**
* 調(diào)用WebService接口垛孔,此方法只訪問過用Java寫的服務(wù)器
*
* @param url WebService服務(wù)器地址
* @param nameSpace 命名空間
* @param methodName WebService的調(diào)用方法名
* @param mapParams WebService的參數(shù)集合,可以為null
* @param reponseCallBack 服務(wù)器響應(yīng)接口
*/
public void call(final String url,
final String nameSpace,
final String methodName,
SimpleArrayMap<String, String> mapParams,
final Response reponseCallBack) {
// 1.創(chuàng)建HttpTransportSE對象施敢,傳遞WebService服務(wù)器地址
final HttpTransportSE transport = new HttpTransportSE(url);
transport.debug = true;
// 2.創(chuàng)建SoapObject對象用于傳遞請求參數(shù)
final SoapObject request = new SoapObject(nameSpace, methodName);
// 2.1.添加參數(shù)也可以不傳
if (mapParams != null) {
for (int index = 0; index < mapParams.size(); index++) {
String key = mapParams.keyAt(index);
String value = mapParams.get(key);
request.addProperty(key, value);
}
}
// 3.實(shí)例化SoapSerializationEnvelope周荐,傳入WebService的SOAP協(xié)議的版本號
final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = isDotNet; // 設(shè)置是否調(diào)用的是.Net開發(fā)的WebService
envelope.setOutputSoapObject(request);
// 4.用于子線程與主線程通信的Handler,網(wǎng)絡(luò)請求成功時會在子線程發(fā)送一個消息僵娃,然后在主線程上接收
final Handler responseHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// 根據(jù)消息的arg1值判斷調(diào)用哪個接口
if (msg.arg1 == SUCCESS_FLAG)
reponseCallBack.onSuccess((SoapObject) msg.obj);
else
reponseCallBack.onError((Exception) msg.obj);
}
};
// 5.提交一個子線程到線程池并在此線種內(nèi)調(diào)用WebService
if (threadPool == null || threadPool.isShutdown())
threadPool = Executors.newFixedThreadPool(threadSize);
threadPool.submit(new Runnable() {
@Override
public void run() {
SoapObject result = null;
try {
// 解決EOFException
System.setProperty("http.keepAlive", "false");
// 連接服務(wù)器
transport.call(null, envelope);
if (envelope.getResponse() != null) {
// 獲取服務(wù)器響應(yīng)返回的SoapObject
result = (SoapObject) envelope.bodyIn;
}
} catch (IOException e) {
// 當(dāng)call方法的第一個參數(shù)為null時會有一定的概念拋IO異常
// 因此需要需要捕捉此異常后用命名空間加方法名作為參數(shù)重新連接
e.printStackTrace();
try {
transport.call(nameSpace + methodName, envelope);
if (envelope.getResponse() != null) {
// 獲取服務(wù)器響應(yīng)返回的SoapObject
result = (SoapObject) envelope.bodyIn;
}
} catch (Exception e1) {
// e1.printStackTrace();
responseHandler.sendMessage(responseHandler.obtainMessage(0, ERROR_FLAG, 0, e1));
}
} catch (XmlPullParserException e) {
// e.printStackTrace();
responseHandler.sendMessage(responseHandler.obtainMessage(0, ERROR_FLAG, 0, e));
} finally {
// 將獲取的消息利用Handler發(fā)送到主線程
responseHandler.sendMessage(responseHandler.obtainMessage(0, SUCCESS_FLAG, 0, result));
}
}
});
}
/**
* 設(shè)置線程池的大小
*
* @param threadSize
*/
public void setThreadSize(int threadSize) {
webServiceUtils.threadSize = threadSize;
threadPool.shutdownNow();
threadPool = Executors.newFixedThreadPool(webServiceUtils.threadSize);
}
public void setIsDotNet(Boolean isDotNet) {
webServiceUtils.isDotNet = isDotNet;
}
/**
* 服務(wù)器響應(yīng)接口概作,在響應(yīng)后需要回調(diào)此接口
*/
public interface Response {
void onSuccess(SoapObject result);
void onError(Exception e);
}
}
三:再次封裝
package com.allens.lib_webservice;
import android.support.v4.util.SimpleArrayMap;
import android.util.Log;
import com.google.gson.Gson;
import org.ksoap2.serialization.SoapObject;
/**
* Created by allens on 2017/12/18.
*/
public class XWebService {
public static <T> void getIntentData(final Class<T> tClass, final String methodName, final String json, final OnResultListener<T> onResultListener) {
SimpleArrayMap<String, String> arrayMap = null;
if (json != null) {
arrayMap = new SimpleArrayMap<>();
arrayMap.put("args", json);
}
WebServiceUtils.create().call("url", "NAMESPACE", methodName, arrayMap, new WebServiceUtils.Response() {
@Override
public void onSuccess(SoapObject result) {
if (result != null) {
String mJson = result.getProperty(0).toString();
Log.i("XWebService", "服務(wù)端獲取的JSON---->" + methodName + "---->" + mJson);
Gson gson = new Gson();
T bean = gson.fromJson(mJson, tClass);
onResultListener.onSuccess(bean);
}
}
@Override
public void onError(Exception e) {
onResultListener.onError();
}
});
}
public interface OnResultListener<T> {
void onSuccess(T bean);
void onError();
}
}
Demo 地址
注意啊,公司不給用默怨,這個是跑不起來的讯榕,只是這里面由上面的那個jar的下載地址
XWebService