不止出现在Android版本低的机型上 查了好多博客 有的说是服务器的原因 但是我觉得应该还是代码原因 请大神指教
private H buildService(int time, boolean isOpenCache) {
//手动创建一个OkHttpClient并设置超时时间
OkHttpClient.Builder builder = new OkHttpClient.Builder();
//日志打印
if (isDebugModel) {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(httpLoggingInterceptor);
}
//添加header
String token =getAppToken();/*getAppToken()*/
if (!TextUtils.isEmpty(token)) {
builder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request()
.newBuilder()
.addHeader("token", token)
.build();
return chain.proceed(request);
}
});
}
if (isOpenCache) {
builder.cache(cache);
builder.addInterceptor(new CacheInterceptor());
builder.addNetworkInterceptor(new CacheInterceptor());
}
if (time > 0) {
if (time == 6)
time = 10;
DEFAULT_TIMEOUT = time;
}
builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
builder.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
builder.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
builder.sslSocketFactory(createSSLSocketFactory());
builder.hostnameVerifier(new TrustAllHostnameVerifier());
OkHttpClient httpClient = builder.build();
Retrofit retrofit = new Retrofit.Builder()
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(baseUrl)
.build();
return retrofit.create(hClass);
}
private static class TrustAllCerts implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
private static class TrustAllHostnameVerifier implements HostnameVerifier {
@Override public boolean verify(String hostname, SSLSession session) {
return true; }
}
private static SSLSocketFactory createSSLSocketFactory() {
SSLSocketFactory ssfFactory = null;
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, new TrustManager[] {
new TrustAllCerts() }, new SecureRandom());
ssfFactory = sc.getSocketFactory();
} catch (Exception e) {
}
return ssfFactory;
}