在Android業(yè)務(wù)開發(fā)中,抓包/mock數(shù)據(jù)一般有兩種方案(該篇主要介紹第二種方案):
一、通過Charles(或fiddle)抓包/mock數(shù)據(jù):
二、添加Intercepter來抓取/mock數(shù)據(jù):
前提:
網(wǎng)絡(luò)庫使用的是okhttp或Retrofit。
這里就用到了okhttp框架的原理定義一個MockDataInterceptor【Mock數(shù)據(jù)攔截器】,并在創(chuàng)建Builder實例的時候直接使用addIntercepter【應(yīng)用攔截器】添加MockDataInterceptor實例即可:
1、addInterceptor(new MockDataInterceptor()):
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(15, TimeUnit.SECONDS)
.writeTimeout(15, TimeUnit.SECONDS)
//在此處添加MockDataInterceptor攔截即可
.addInterceptor(new MockDataInterceptor())
.retryOnConnectionFailure(false);
?2、MockDataInterceptor定義如下:
/**
* Mock數(shù)據(jù)攔截器
*/
public class MockDataInterceptor implements Interceptor {
private static final String TAG = "MockDataInterceptor";
//是否開啟mock數(shù)據(jù)
private static final boolean mockIsOpen = true;
private String path;
private static Map<String, String> pathMap;
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
Headers headers = response.request().headers();
if (shouldInterceptorRequest(request) && mockIsOpen) {
//查看Headers請求參數(shù)
Log.d(TAG, "headers:" + headers);
//查看原始responseBody數(shù)據(jù)
Response.Builder builder = response.newBuilder();
if (response.body() != null) {
String originalBody = response.body().string();
Log.d(TAG, "原始的 responseBody:" + originalBody);
}
if (!TextUtils.isEmpty(getMockData())) {
//為responseBody賦值[mock數(shù)據(jù)]
ResponseBody responseBody = ResponseBody.create(MediaType.parse("application/json; charset=utf-8"), getMockData());
builder.body(responseBody);
Log.d(TAG, "mock數(shù)據(jù)后 responseBody:" + getMockData());
}
return builder.build();
} else {
return response;
}
}
//獲取對應(yīng)的mock數(shù)據(jù)
private String getMockData() {
for (String key : pathMap.keySet()) {
if (path.contains(key)) {
return pathMap.get(key);
}
}
return "";
}
//在此處添加被攔截的接口判斷
private boolean shouldInterceptorRequest(Request request) {
path = request.url().url().getPath();
for (String key : pathMap.keySet()) {
if (path.contains(key)) {
Log.d(TAG, "path:" + path);
return true;
}
}
return false;
}
public static final String MOCK_DATA_APP_***_*HECK = "{\n" +
" \"resMsg\":{\n" +
" \"code\":0,\n" +
" \"message\":\"\\u6210\\u529f\"\n" +
" },\n" +
" \"datas\":{\n" +
" \"type\":1,\n" +
" \"title\":\"\標題",\n" +
" \"content\":\"哈哈哈",\n" +
" \"url\":\"\"\n" +
" }\n" +
"}";
//將對應(yīng)的接口path及該接口mock數(shù)據(jù)添加到pathMap中
static {
pathMap = new HashMap<>();
pathMap.put("app****heck", MOCK_DATA_APP_***_*HECK);
}
}
使用前需注意:文章來源:http://www.zghlxwxcb.cn/news/detail-727303.html
- mock開關(guān)是打開的mockIsOpen = true;
- pathMap中已添加所需的接口path及mock數(shù)據(jù)。
這樣就可以愉快的抓包及mock數(shù)據(jù)了。文章來源地址http://www.zghlxwxcb.cn/news/detail-727303.html
到了這里,關(guān)于Android OkHttp/Retrofit框架使用Interceptor 抓包/mock數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!