正在查看: 大众借. v4.1.2 应用的 Api2.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
正在查看: 大众借. v4.1.2 应用的 Api2.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
package com.shiliuj.mjyp.app.api;
import com.base.commonlibrary.BaseApplication;
import com.base.commonlibrary.netstate.ComNetWorkUtil;
import com.base.commonlibrary.utils.ApplicationUtil;
import com.base.commonlibrary.utils.SPUtils;
import com.google.gson.GsonBuilder;
import com.shiliuj.mjyp.app.api.AppConfig;
import com.shiliuj.mjyp.app.utils.AppUtils;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import okio.BufferedSink;
import okio.GzipSink;
import okio.Okio;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class Api2 {
private static final String CACHE_CONTROL_AGE = "max-age=0";
private static final String CACHE_CONTROL_CACHE = "only-if-cached, max-stale=172800";
private static final long CACHE_STALE_SEC = 172800;
public static final int CONNECT_TIME_OUT = 30000;
public static final int READ_TIME_OUT = 30000;
private static Api2 retrofitManager;
public ApiService movieService;
public Retrofit retrofit;
private final Interceptor mRewriteCacheControlInterceptor = new Interceptor() {
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
if (!ComNetWorkUtil.isNetworkAvailable(BaseApplication.getContext())) {
request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
}
Response proceed = chain.proceed(request);
if (ComNetWorkUtil.isNetworkAvailable(BaseApplication.getContext())) {
return proceed.newBuilder().header("Cache-Control", request.cacheControl().toString()).removeHeader("Pragma").build();
}
return proceed.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=172800").removeHeader("Pragma").build();
}
};
private final Interceptor headerInterceptor = new Interceptor() {
public Response intercept(Interceptor.Chain chain) throws IOException {
return chain.proceed(chain.request().newBuilder().addHeader("Content-Type", "application/json;charset=UTF-8").addHeader("token", (String) SPUtils.getData(AppConfig.CACHE.Token, "")).addHeader("ClientType", "android").addHeader("ClientVersion", ApplicationUtil.getAPPVersion(BaseApplication.getContext())).build());
}
};
private Api2() {
if (AppUtils.INSTANCE.isVpnUsed() || AppUtils.INSTANCE.isWifiProxy()) {
Retrofit build = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").serializeNulls().create())).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).client(getOkHttpClient()).baseUrl("http://www.beizhuabao.com").build();
this.retrofit = build;
this.movieService = (ApiService) build.create(ApiService.class);
return;
}
Retrofit build2 = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").serializeNulls().create())).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).client(getOkHttpClient()).baseUrl(AppConfig.BASE_URL).build();
this.retrofit = build2;
this.movieService = (ApiService) build2.create(ApiService.class);
}
private OkHttpClient getOkHttpClient() {
new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);
return new OkHttpClient.Builder().readTimeout(30000L, TimeUnit.MILLISECONDS).connectTimeout(30000L, TimeUnit.MILLISECONDS).addInterceptor(this.mRewriteCacheControlInterceptor).addInterceptor(new GzipRequestInterceptor()).addNetworkInterceptor(this.mRewriteCacheControlInterceptor).addInterceptor(this.headerInterceptor).cache(new Cache(new File(BaseApplication.getContext().getCacheDir(), "cache"), 104857600L)).build();
}
public static ApiService getDefault() {
Api2 api2 = new Api2();
retrofitManager = api2;
return api2.movieService;
}
public static String getCacheControl() {
return ComNetWorkUtil.isNetworkAvailable(BaseApplication.getInstance()) ? CACHE_CONTROL_AGE : CACHE_CONTROL_CACHE;
}
static class GzipRequestInterceptor implements Interceptor {
GzipRequestInterceptor() {
}
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
if (request.body() == null || request.header("Content-Encoding") != null) {
return chain.proceed(request);
}
return chain.proceed(request.newBuilder().header("Content-Encoding", "gzip").method(request.method(), gzip(request.body())).build());
}
private RequestBody gzip(final RequestBody requestBody) {
return new RequestBody() {
public long contentLength() {
return -1L;
}
public MediaType contentType() {
return requestBody.contentType();
}
public void writeTo(BufferedSink bufferedSink) throws IOException {
BufferedSink buffer = Okio.buffer(new GzipSink(bufferedSink));
requestBody.writeTo(buffer);
buffer.close();
}
};
}
}
}