| | |
| | | |
| | | import android.content.Context; |
| | | |
| | | import androidx.annotation.NonNull; |
| | | |
| | | import com.android.app_base.BuildConfig; |
| | | import com.android.app_base.base.BaseApplication; |
| | | import com.android.app_base.http.interceptor.AuthInterceptor; |
| | | import com.android.app_base.http.interceptor.CacheInterceptor; |
| | | import com.android.app_base.http.interceptor.LogInterceptor; |
| | | import com.blankj.utilcode.util.LogUtils; |
| | | import com.franmontiel.persistentcookiejar.PersistentCookieJar; |
| | | import com.franmontiel.persistentcookiejar.cache.SetCookieCache; |
| | | import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.net.URLDecoder; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | import okhttp3.Cache; |
| | | import okhttp3.Interceptor; |
| | | import okhttp3.OkHttpClient; |
| | | import okhttp3.Request; |
| | | import okhttp3.Response; |
| | | import okhttp3.logging.HttpLoggingInterceptor; |
| | | |
| | | /** |
| | |
| | | private final static int WRITE_TIMEOUT = 15; |
| | | //缓存时间 |
| | | private final static int CACHE_SIZE = 10 * 1024 * 1024; |
| | | private static Context mContext = BaseApplication.getInstance(); |
| | | private static volatile OkHttpClient okHttpClient; |
| | | |
| | | private OkHttpHelper() { |
| | |
| | | //错误重连 |
| | | clientBuilder.retryOnConnectionFailure(true); |
| | | //cookie |
| | | Context mContext = BaseApplication.getInstance(); |
| | | clientBuilder.cookieJar(new PersistentCookieJar(new SetCookieCache(),new SharedPrefsCookiePersistor(mContext))); |
| | | |
| | | //缓存拦截 |
| | | Cache cache = new Cache(new File(mContext.getCacheDir(),"http_cache"),CACHE_SIZE); |
| | | clientBuilder.cache(cache).addInterceptor(new CacheInterceptor()); |
| | | |
| | | // //增加头部信息拦截 |
| | | // clientBuilder.addInterceptor(new Interceptor() { |
| | | // @Override |
| | | // public Response intercept(Chain chain) throws IOException { |
| | | // Request build = chain.request().newBuilder() |
| | | // .addHeader("Content-Type", "application/json") |
| | | // .build(); |
| | | // return chain.proceed(build); |
| | | // } |
| | | // }); |
| | | //增加头部信息拦截 |
| | | clientBuilder.addInterceptor(new Interceptor() { |
| | | @NonNull |
| | | @Override |
| | | public Response intercept(@NonNull Chain chain) throws IOException { |
| | | Request build = chain.request().newBuilder() |
| | | .addHeader("Content-Type", "application/json") |
| | | .build(); |
| | | return chain.proceed(build); |
| | | } |
| | | }); |
| | | clientBuilder.addInterceptor(new LogInterceptor());//拦截器添加公共参数 |
| | | clientBuilder.addInterceptor(new AuthInterceptor());//拦截器token失效处理 |
| | | //log日志拦截 |
| | | if (BuildConfig.DEBUG) { |
| | | clientBuilder.addInterceptor(new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() { |
| | | @Override |
| | | public void log(String message) { |
| | | try { |
| | | // String msg = message.replaceAll("%(?![0-9a-fA-F]{2})","%25"); |
| | | String text = URLDecoder.decode(message, "utf-8"); |
| | | String msg = message.replaceAll("%(?![0-9a-fA-F]{2})","%25"); |
| | | String text = URLDecoder.decode(msg, "utf-8"); |
| | | LogUtils.dTag("LOG","网络请求"+text); |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | |
| | | } |
| | | return okHttpClient; |
| | | } |
| | | |
| | | public static void addInterceptor(Interceptor interceptor){ |
| | | okHttpClient = getOkHttpClient().newBuilder().addInterceptor(interceptor).build(); |
| | | } |
| | | } |