我正在使用retrofit和okHttp来与服务器通信。问题是android 5 gettting错误,这是在Log ..一切都像下一个Android版本的魅力。我试着信任所有的证书,但仍然得到这个错误......任何想法?
设置改造:
RestAdapter.Builder builder = new RestAdapter.Builder()
.setEndpoint(config.BackendConfig.API_URL)
.setRequestInterceptor(requestInterceptor)
.setLogLevel(GeneralConfig.RETROFIT_LOG_LEVEL);
builder.setClient(new OkClient(UnsafeOkHttpClientUtil.getClient()));
RestAdapter restAdapter = builder.build();
sApiService = restAdapter.create(CervApiService.class);
设置UnsafeOkHttpClientUtil:
try {
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {}
@Override
public java.security.cert.X509Certificate[]getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setSslSocketFactory(sslSocketFactory);
okHttpClient.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
return okHttpClient;
日志:
cz.myapp.app W/System.err﹕ retrofit.RetrofitError: Connection closed by peer
cz.myapp.app W/System.err﹕ at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:390)
cz.myapp.app W/System.err﹕ at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
cz.myapp.app W/System.err﹕ at java.lang.reflect.Proxy.invoke(Proxy.java:397)
cz.myapp.app W/System.err﹕ at $Proxy1.regContract(Unknown Source)
cz.myapp.app W/System.err﹕ at cz.myapp.app.loader.api.POSTContractLoader.callApi(POSTContractLoader.java:33)
cz.myapp.app W/System.err﹕ at cz.myapp.app.loader.base.BaseRestLoader.loadInBackground(BaseRestLoader.java:61)
cz.myapp.app W/System.err﹕ at cz.myapp.app.loader.base.BaseRestLoader.loadInBackground(BaseRestLoader.java:26)
cz.myapp.app W/System.err﹕ at android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:242)
cz.myapp.app W/System.err﹕ at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:51)
cz.myapp.app W/System.err﹕ at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:40)
cz.myapp.app W/System.err﹕ at android.support.v4.content.ModernAsyncTask$2.call(ModernAsyncTask.java:123)
cz.myapp.app W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
cz.myapp.app W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
cz.myapp.app W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
cz.myapp.app W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
cz.myapp.app W/System.err﹕ Caused by: javax.net.ssl.SSLHandshakeException: Connection closed by peer
cz.myapp.app W/System.err﹕ at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
cz.myapp.app W/System.err﹕ at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:318)
cz.myapp.app W/System.err﹕ at com.squareup.okhttp.Connection.upgradeToTls(Connection.java:242)
cz.myapp.app W/System.err﹕ at com.squareup.okhttp.Connection.connect(Connection.java:159)
cz.myapp.app W/System.err﹕ at com.squareup.okhttp.Connection.connectAndSetOwner(Connection.java:175)
cz.myapp.app W/System.err﹕ at com.squareup.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:120)
cz.myapp.app W/System.err﹕ at com.squareup.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:330)
cz.myapp.app W/System.err﹕ at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:319)
cz.myapp.app W/System.err﹕ at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)
cz.myapp.app W/System.err﹕ at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:425)
cz.myapp.app W/System.err﹕ at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:110)
cz.myapp.app W/System.err﹕ at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:241)
cz.myapp.app W/System.err﹕ at com.squareup.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
cz.myapp.app W/System.err﹕ at com.squareup.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
cz.myapp.app W/System.err﹕ at retrofit.client.UrlConnectionClient.prepareRequest(UrlConnectionClient.java:68)
cz.myapp.app W/System.err﹕ at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:37)
cz.myapp.app W/System.err﹕ at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
cz.myapp.app W/System.err﹕ ... 14 more