okhttp3 快取如此簡單
阿新 • • 發佈:2019-01-01
不需要新增addNetworkInterceptor
OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
mOkHttpClient.addInterceptor(new BasicParamsInterceptor(mCacheTime))
public class BasicParamsInterceptor implements Interceptor { private Map<String, String> queryParamsMap = new HashMap<>(); privateMap<String, String> headerParamsMap = new HashMap<>(); private int mTime; public BasicParamsInterceptor(int mTime) { this.mTime = mTime; } @Override public Response intercept(Chain chain) throws IOException { if (!NetworkUtils.hasNetwork()) { returngetNetworkResponse(chain); } return getHttpResponse(chain); } /** * 新增header * * @param request * @param requestBuilder * @param paramsMap */ private void injectHeaderIntoUrl(Request request, Request.Builder requestBuilder, Map<String, String> paramsMap) { Headers.Builder headerBuilder = request.headers().newBuilder(); Iterator iterator = paramsMap.entrySet().iterator(); while(iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); headerBuilder.add((String) entry.getKey(), (String) entry.getValue()); } requestBuilder.headers(headerBuilder.build()); } /** * 新增paramsMap * * @param request * @param requestBuilder * @param paramsMap */ private void injectParamsIntoUrl(Request request, Request.Builder requestBuilder, Map<String, String> paramsMap) { HttpUrl.Builder httpUrlBuilder = request.url().newBuilder(); if (paramsMap.size() > 0) { Iterator iterator = paramsMap.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); httpUrlBuilder.addQueryParameter((String) entry.getKey(), (String) entry.getValue()); } } requestBuilder.url(httpUrlBuilder.build()); } private Request getRequest(Chain chain) throws IOException { Request request = chain.request(); Request.Builder requestBuilder = request.newBuilder(); addParamsInterceptor(); if (headerParamsMap.size() > 0) { injectHeaderIntoUrl(request, requestBuilder, headerParamsMap); } if (queryParamsMap.size() > 0) { injectParamsIntoUrl(request, requestBuilder, queryParamsMap); } request = requestBuilder.build(); return request; } /** * 讀取網路資訊並快取 * * @param chain * @return * @throws IOException */ private Response getHttpResponse(Chain chain) throws IOException { return chain.proceed(getRequest(chain)); } /** * 讀取本地快取資訊 * * @param chain * @return * @throws IOException */ private Response getNetworkResponse(Chain chain) throws IOException { Request request = getRequest(chain); request = request.newBuilder() .cacheControl(CacheControl.FORCE_CACHE) .build(); return chain.proceed(request); } /** * 新增頭部攔截資訊 */ public void addParamsInterceptor() { queryParamsMap.clear(); headerParamsMap.clear(); String token_type = ""; String access_token = ""; if (AccountManager.getInstance().getAccountToken().getToken_type() != null) { token_type = AccountManager.getInstance().getAccountToken().getToken_type(); } if (AccountManager.getInstance().getAccountToken().getAccess_token() != null) { access_token = AccountManager.getInstance().getAccountToken().getAccess_token(); } headerParamsMap.put("User-Agent", "android-xiaodao-agent"); headerParamsMap.put(ArgConstants.AUTHORIZATION, String.format("%s %s", token_type, access_token)); headerParamsMap.put("Cache-Control", "public, max-age=" + mTime); String mAppVersion = AppStructure.getInstance().getAppVersion(); queryParamsMap.put("__version", !TextUtils.isEmpty(mAppVersion) ? mAppVersion : ""); queryParamsMap.put("os", Build.PRODUCT); // queryParamsMap.put("net", NetworkUtils.GetNetworkType()); queryParamsMap.put("_platform", "android"); } }