Search in sources :

Example 1 with NetworkHandler

use of com.tool.common.http.NetworkHandler 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;
        }
    });
}
Also used : User(com.frame.mvp.entity.User) OkHttpClient(okhttp3.OkHttpClient) HashMap(java.util.HashMap) GsonBuilder(com.google.gson.GsonBuilder) GsonResponseDeserializer(com.frame.mvp.app.utils.gson.GsonResponseDeserializer) TrustAllX509TrustManager(com.tool.common.http.ssl.TrustAllX509TrustManager) NetworkHandler(com.tool.common.http.NetworkHandler) List(java.util.List) ParameterInterceptor(com.tool.common.http.interceptor.ParameterInterceptor) Context(android.content.Context) Cookie(okhttp3.Cookie) LoggingInterceptor(com.tool.common.http.interceptor.LoggingInterceptor) GsonBuilder(com.google.gson.GsonBuilder) PersistentCookieJar(com.tool.common.http.cookie.PersistentCookieJar) Request(okhttp3.Request) HttpModule(com.tool.common.di.module.HttpModule) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) TrustAllHostnameVerifier(com.tool.common.http.ssl.TrustAllHostnameVerifier) File(java.io.File)

Aggregations

Context (android.content.Context)1 GsonResponseDeserializer (com.frame.mvp.app.utils.gson.GsonResponseDeserializer)1 User (com.frame.mvp.entity.User)1 GsonBuilder (com.google.gson.GsonBuilder)1 HttpModule (com.tool.common.di.module.HttpModule)1 NetworkHandler (com.tool.common.http.NetworkHandler)1 PersistentCookieJar (com.tool.common.http.cookie.PersistentCookieJar)1 LoggingInterceptor (com.tool.common.http.interceptor.LoggingInterceptor)1 ParameterInterceptor (com.tool.common.http.interceptor.ParameterInterceptor)1 TrustAllHostnameVerifier (com.tool.common.http.ssl.TrustAllHostnameVerifier)1 TrustAllX509TrustManager (com.tool.common.http.ssl.TrustAllX509TrustManager)1 File (java.io.File)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Cookie (okhttp3.Cookie)1 HttpUrl (okhttp3.HttpUrl)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1