public abstract class BaseApi {
public static final String API_SERVER = "服务器地址"
private static final OkHttpClient mOkHttpClient = new OkHttpClient();
private static Retrofit mRetrofit;
protected static Retrofit getRetrofit() {
if (Retrofit == null) {
Context context = Application.getInstance().getApplicationContext();
mOkHttpClient.setConnectTimeout(30, TimeUnit.SECONDS);
mOkHttpClient.networkInterceptors()
.add(new CookiesInterceptor(context));
File cacheDirectory = new File(context.getCacheDir()
.getAbsolutePath(), "HttpCache");
Cache cache = new Cache(cacheDirectory, 20 * 1024 * 1024);
mOkHttpClient.setCache(cache);
mRetrofit = new Retrofit.Builder()
.baseUrl(API_SERVER + "/")
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.addConverterFactory(ResponseConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(mOkHttpClient)
.build();
}
return mRetrofit;
}
}