use of com.tool.common.http.interceptor.ParameterInterceptor in project MVPFrames by RockyQu.
the class AppConfiguration method applyOptions.
@Override
public void applyOptions(final Context context, AppConfigModule.Builder builder) {
builder.httpUrl(Api.APP_DOMAIN).cacheFile(new File(ProjectUtils.CACHE)).networkHandler(new // Http全局响应结果的处理类
NetworkHandler() {
@Override
public Request onHttpRequest(Interceptor.Chain chain, Request request) {
// return chain.request().newBuilder().header("token", tokenId).build();
return request;
}
@Override
public Response onHttpResponse(String result, Interceptor.Chain chain, Request request, Response response) {
return response;
}
}).interceptors(new Interceptor[] { new LoggingInterceptor(), new ParameterInterceptor(new ParameterInterceptor.ParameterCallback() {
/**
* 这里为接口添加类型为HashMap的统一参数,例如Token、版本号等。支持Get、Post方法,ParameterInterceptor会自动判断
*/
@Override
public HashMap<String, Object> parameters() {
User user = (User) ((App) context).getAppComponent().extras().get(LoginActivity.class.getName());
HashMap<String, Object> parameters = new HashMap<>();
if (user != null) {
// 为接口统一添加access_token参数
parameters.put("access_token", user.getToken());
}
return parameters;
}
}) }).retrofitConfiguration(new // 扩展自定义配置Retrofit参数
HttpModule.RetrofitConfiguration() {
@Override
public void configRetrofit(Context context, Retrofit.Builder builder) {
}
}).okHttpConfiguration(new // 扩展自定义配置OkHttp参数
HttpModule.OkHttpConfiguration() {
@Override
public void configOkHttp(Context context, OkHttpClient.Builder builder) {
builder.sslSocketFactory(SSL.createSSLSocketFactory(), new TrustAllX509TrustManager());
builder.hostnameVerifier(new TrustAllHostnameVerifier());
}
}).gsonConfiguration(new // 扩展自定义配置Gson参数
AppModule.GsonConfiguration() {
@Override
public void configGson(Context context, GsonBuilder builder) {
builder.serializeNulls().registerTypeAdapter(ResponseEntity.class, new GsonResponseDeserializer());
}
}).cookieLoadForRequest(new PersistentCookieJar.CookieLoadForRequest() {
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
}
@Override
public List<Cookie> loadForRequest(List<Cookie> cookies) {
return cookies;
}
});
}
Aggregations