return retrofit.create(cls);
}
}
RetrofitUtils用来构建Retrofit.Builder,并对OkHttp做以下几个方面的配置:
1. 设置日志拦截器,拦截服务器返回的json数据。Retrofit将请求到json数据直接转换成了实体类,但有时候我们需要查看json数据,Retrofit并没有提供直接获取json数据的功能。因此我们需要自定义一个日志拦截器拦截json数据,并输入到控制台。
2. 设置Http请求头。给OkHttp 添加请求头拦截器,配置请求头信息。还可以为接口统一添加请求头数据。例如,把用户名、密码(或者token)统一添加到请求头。后续每个接口的请求头中都会携带用户名、密码(或者token)数据,避免了为每个接口单独添加。
3. 为OkHttp配置缓存。同样可以同过拦截器实现缓存处理。包括控制缓存的最大生命值,控制缓存的过期时间。
4. 如果采用https,我们还可以在此处理证书校验以及服务器校验。
5. 为Retrofit添加GsonConverterFactory。此处是一个比较重要的环节,将在后边详细讲解。
RetrofitUtils 代码如下:
public class RetrofitUtils {
public static OkHttpClient.Builder getOkHttpClientBuilder() {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
try {
LogUtils.e("OKHttp-----", URLDecoder.decode(message, "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
LogUtils.e("OKHttp-----", message);
}
}
});
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
File cacheFile = new File(Utils.getContext().getCacheDir(), "cache");
Cache cache = new Cache(cacheFile, 1024 * 1024 * 100); //100Mb
return new OkHttpClient.Builder()
.readTimeout(Constants.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
.connectTimeout(Constants.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
.addInterceptor(loggingInterceptor)
.addInterceptor(new HttpHeaderInterceptor())
.addNetworkInterceptor(new HttpCacheInterceptor())
// .sslSocketFactory(SslContextFactory.getSSLSocketFactoryForTwoWay()) // https认证 如果要使用https且为自定义证书 可以去掉这两行注释,并自行配制证书。
// .hostnameVerifier(new SafeHostnameVerifier())
.cache(cache);
}
public static Retrofit.Builder getRetrofitBuilder(String baseUrl) {
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").serializeNulls().create();
OkHttpClient okHttpClient = RetrofitUtils.getOkHttpClientBuilder().build();
return new Retrofit.Builder()
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJa