前言:
最近在做android开发,在访问webservice时,感觉过于繁琐。所以想重新封装一个访问webservice.当前比较流行的网络访问框架当属 Retrofit + Okhttp + RxJava .所以决定入手一款。
参考文章: https://www.jianshu.com/p/b865c855a1e8
盘他:
1.定义了一个参数配置类: WebServiceConfigManager
主要负责设置相关参数: webservice地址 ,命名空间,访问超时时间等参数
package com.qj.webservice;
/**
*
*
* __----~~~~~~~~~~~------___
* . . ~~//====...... __--~ ~~
* -. \_|// |||\\ ~~~~~~::::... /~
* ___-==_ _-~o~ \/ ||| \\ _/~~-
* __---~~~.==~||\=_ -_--~/_-~|- |\\ \\ _/~
* _-~~ .=~ | \\-_ '-~7 /- / || \ /
* .~ .~ | \\ -_ / /- / || \ /
* / ____ / | \\ ~-_/ /|- _/ .|| \ /
* |~~ ~~|--~~~~--_ \ ~==-/ | \~--===~~ .\
* ' ~-| /| |-~\~~ __--~~
* |-~~-_/ | | ~\_ _-~ /\
* / \ \__ \/~ \__
* _--~ _/ | .-~~____--~-/ ~~==.
* ((->/~ '.|||' -_| ~~-/ , . _||
* -_ ~\ ~~---l__i__i__i--~~_/
* _-~-__ ~) \--______________--~~
* //.-~~~-~_--~- |-------~~~~~~~~
* //.-~~~--\
* 神兽保佑
* 代码无BUG!
*
*
* @des Webservice 配置管理类 (请求前需配置相关参数)
* @author qujun
* @time 2019/1/19 20:26
*/
public class WebServiceConfigManager {
private static WebServiceConfigManager.Builder mBuilder = null;
public WebServiceConfigManager(WebServiceConfigManager.Builder builder) {
mBuilder = builder;
}
public static WebServiceConfigManager.Builder getBuilder() {
if (mBuilder == null)
throw new RuntimeException("Related parameters need to be configured before request..");
return mBuilder;
}
public static class Builder {
/*
* WebService命名空间
* */
public String NAME_SPACE = "";
/*
* WebService访问地址
* */
public String BASE_URL = "";
/*
* WebService HEAD_PAGE
* */
public String HEAD_PAGE = "";
/*
* WebService 连接超时时间 (默认20s)
* */
public int CONNECT_TIMEOUT = 20; // ws 连接超时时间
/*
* WebService 读取超时时间(默认20s)
* */
public int READ_TIMEOUT = 20;
/*
* WebService 写入超时时间(默认20s)
* */
public int WRITE_TIMEOUT = 20;
/*
* WebService 缓存路径
* */
public String CACHE_PATH = "webservice/cache";
/*
* WebService 缓存大小
* */
public int CACHE_SIZE = 32 * 1024 * 1024;
public Builder() {
}
public Builder setNAME_SPACE(String NAME_SPACE) {
this.NAME_SPACE = NAME_SPACE;
return this;
}
public Builder setBASE_URL(String BASE_URL) {
this.BASE_URL = BASE_URL;
return this;
}
public Builder setHEAD_PAGE(String HEAD_PAGE) {
this.HEAD_PAGE = HEAD_PAGE;
return this;
}
public Builder setCONNECT_TIMEOUT(int CONNECT_TIMEOUT) {
this.CONNECT_TIMEOUT = CONNECT_TIMEOUT;
return this;
}
public Builder setCACHE_PATH(String CACHE_PATH) {
this.CACHE_PATH = CACHE_PATH;
return this;
}
public Builder setREAD_TIMEOUT(int READ_TIMEOUT) {
this.READ_TIMEOUT = READ_TIMEOUT;
return this;
}
public Builder setWRITE_TIMEOUT(int WRITE_TIMEOUT) {
this.WRITE_TIMEOUT = WRITE_TIMEOUT;
return this;
}
public Builder setCACHE_SIZE(int CACHE_SIZE) {
this.CACHE_SIZE = CACHE_SIZE;
return this;
}
public WebServiceConfigManager build() {
return new WebServiceConfigManager(this);
}
}
}
2. 定义一个webservice访问基类 : BaseWebService 只需继承该类即可
package com.qj.webservice;
/*
*
*
* __----~~~~~~~~~~~------___
* . . ~~//====...... __--~ ~~
* -. \_|// |||\\ ~~~~~~::::... /~
* ___-==_ _-~o~ \/ ||| \\ _/~~-
* __---~~~.==~||\=_ -_--~/_-~|- |\\ \\ _/~
* _-~~ .=~ | \\-_ '-~7 /- / || \ /
* .~ .~ | \\ -_ / /- / || \ /
* / ____ / | \\ ~-_/ /|- _/ .|| \ /
* |~~ ~~|--~~~~--_ \ ~==-/ | \~--===~~ .\
* ' ~-| /| |-~\~~ __--~~
* |-~~-_/ | | ~\_ _-~ /\
* / \ \__ \/~ \__
* _--~ _/ | .-~~____--~-/ ~~==.
* ((->/~ '.|||' -_| ~~-/ , . _||
* -_ ~\ ~~---l__i__i__i--~~_/
* _-~-__ ~) \--______________--~~
* //.-~~~-~_--~- |-------~~~~~~~~
* //.-~~~--\
* 神兽保佑
* 代码无BUG!
*
* @author QuJun
* @time 2019/01/19 16:11
*
**/
import java.util.HashMap;
import java.util.Map;
import okhttp3.ResponseBody;
import retrofit2.Call;
public abstract class BaseWebService {
private String xmlStr = "";
private String method = "";
private Map valuesMap = null;
private Call call = null;
protected BaseWebService() {
valuesMap = new HashMap<>();
}
private void init() {
method = setMethod();
valuesMap = setParamMap();
initXml();
}
private void initXml() {
xmlStr = "\n"
+ " \n"
+ " \n "
+ " <" + method + " xmlns=\"" + WebServiceConfigManager.getBuilder().NAME_SPACE + "\">\n"
+ getNodeValue() + "\n"
+ " " + method + ">\n"
+ " \n"
+ " \n";
}
private String getNodeValue() {
StringBuilder str = new StringBuilder();
for (Map.Entry entry : valuesMap.entrySet()) {
str.append(toStart(entry.getKey())).append(entry.getValue()).append(toEnd(entry.getKey()));
}
return str.toString();
}
private String toStart(String key) {
return "<" + key + ">";
}
private String toEnd(String key) {
return "" + key + ">";
}
private String getXml() {
return xmlStr;
}
/*
* 处理请求 (必须先设置请求方法名)
*
* */
@SuppressWarnings("unchecked")
public void doRequest(final RequestCallBack callBack) {
try {
init();
if (method.equals("")) {
throw new RuntimeException("Method is NULL.");
}
RequestManager rm = RequestManager.getInstance();
WsServiceImpl impl = rm.apiService;
// 添加请求头
Map headers = new HashMap<>();
headers.put("Content-Type", "text/xml; charset=utf-8");
headers.put("SOAPAction", WebServiceConfigManager.getBuilder().NAME_SPACE + method);
call = impl.getResponBody(
headers,
WebServiceConfigManager.getBuilder().HEAD_PAGE,
method,
getXml());
rm.execute(call, method, callBack);
} catch (Exception ex) {
ex.printStackTrace();
if (callBack != null) {
callBack.onError(WS_REQUEST_RESULT.REQUEST_NOT_SERVER, "无法连接服务器");
callBack.onFinish();
}
}
}
/*
* 停止请求
* */
public void cancelRequest() {
if (call != null && !call.isCanceled()) {
call.cancel();
}
}
public abstract Map setParamMap();
public abstract String setMethod();
}
3. 定义 RequestManager 负责处理webservice 请求
package com.qj.webservice;
import android.os.Environment;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
/**
*
*
* __----~~~~~~~~~~~------___
* . . ~~//====...... __--~ ~~
* -. \_|// |||\\ ~~~~~~::::... /~
* ___-==_ _-~o~ \/ ||| \\ _/~~-
* __---~~~.==~||\=_ -_--~/_-~|- |\\ \\ _/~
* _-~~ .=~ | \\-_ '-~7 /- / || \ /
* .~ .~ | \\ -_ / /- / || \ /
* / ____ / | \\ ~-_/ /|- _/ .|| \ /
* |~~ ~~|--~~~~--_ \ ~==-/ | \~--===~~ .\
* ' ~-| /| |-~\~~ __--~~
* |-~~-_/ | | ~\_ _-~ /\
* / \ \__ \/~ \__
* _--~ _/ | .-~~____--~-/ ~~==.
* ((->/~ '.|||' -_| ~~-/ , . _||
* -_ ~\ ~~---l__i__i__i--~~_/
* _-~-__ ~) \--______________--~~
* //.-~~~-~_--~- |-------~~~~~~~~
* //.-~~~--\
* 神兽保佑
* 代码无BUG!
*
*
* @des 请求处理 ( Retrofit2+Okhttp3 通过SOAP协议请求WebService )
* @author qujun
* @time 2019/1/19 22:37
*/
public class RequestManager {
private static RequestManager manager;//管理者实例
WsServiceImpl apiService = null;
private String webserviceResult = "";
static synchronized RequestManager getInstance() {
if (manager == null)
manager = new RequestManager();
return manager;
}
private RequestManager() {
build();
}
private void build() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
File httpCacheDirectory = new File(Environment.getExternalStorageDirectory(), WebServiceConfigManager.getBuilder().CACHE_PATH);
Cache cache = new Cache(httpCacheDirectory, WebServiceConfigManager.getBuilder().CACHE_SIZE);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.readTimeout(WebServiceConfigManager.getBuilder().READ_TIMEOUT, TimeUnit.SECONDS);
builder.writeTimeout(WebServiceConfigManager.getBuilder().WRITE_TIMEOUT, TimeUnit.SECONDS);
builder.connectTimeout(WebServiceConfigManager.getBuilder().CONNECT_TIMEOUT, TimeUnit.SECONDS);
builder.retryOnConnectionFailure(true);
builder.addInterceptor(interceptor);
builder.cache(cache);
OkHttpClient mClient = builder.build();
String BASE_URl = WebServiceConfigManager.getBuilder().BASE_URL;
Retrofit mRetrofit = new Retrofit.Builder()
.baseUrl(BASE_URl)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(mClient)
.build();
apiService = mRetrofit.create(WsServiceImpl.class);
}
/**
* 访问webservice
*/
void execute(Call call, final String method, final RequestCallBack callBack) {
callBack.onStart();
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if (response.body() == null) {
callBack.onError(WS_REQUEST_RESULT.REQUEST_ERROR, "请求发生错误..");
callBack.onFinish();
return;
}
try {
if (analysisResult(response.body().string(), method)) {
callBack.onSuccess(webserviceResult);
return;
}
} catch (IOException e) {
e.printStackTrace();
}
callBack.onError(WS_REQUEST_RESULT.REQUEST_RESULT_ERROR, "请求结果xml解析错误..");
callBack.onFinish();
}
@Override
public void onFailure(Call call, Throwable t) {
callBack.onError(WS_REQUEST_RESULT.REQUEST_NET_ERROR, t.toString());
callBack.onFinish();
}
});
}
// 解析xml
private boolean analysisResult(String response, final String method) {
String startTag = "<" + method + "Result" + ">";
String endTag = "" + method + "Result" + ">";
if (response.contains(startTag) && response.contains(endTag)) {
int startIndex = response.indexOf(startTag) + startTag.length();
int endIndex = response.lastIndexOf(endTag);
webserviceResult = response.substring(startIndex, endIndex);
return true;
}
return false;
}
/*
* 清除实例
* */
public void clear() {
if (manager != null)
manager = null;
if (apiService != null)
apiService = null;
}
}
使用:
1. 配置相关webservice参数:
new WebServiceConfigManager.Builder()
.setBASE_URL("http://www.webxml.com.cn/WebServices/")
.setNAME_SPACE("http://WebXml.com.cn/")
.setHEAD_PAGE("WeatherWebService.asmx")
.build();
2. 创建访问webservice
public class TestWs extends BaseWebService {
private String byProvinceName = "";
public TestWs(String byProvinceName) {
this.byProvinceName = byProvinceName;
}
@Override
public Map setParamMap() {
Map map = new HashMap<>();
map.put("byProvinceName", byProvinceName);
return map;
}
@Override
public String setMethod() {
return "getSupportCity";
}
}
3. 开始盘他
TestWs ws = new TestWs("北京");
ws.doRequest(new RequestCallBack() {
@Override
public void onSuccess(String data) {
txt.setText(data);
}
@Override
public void onError(int result, String msg) {
txt.setText(msg);
}
@Override
public void onStart() {
}
@Override
public void onFinish() {
}
});
结语:
文采不够美丽,如有问题欢迎留言........
DEMO地址: https://download.csdn.net/download/haiyangyunbao813/10928729