Android 报错解决办法:I/O error during system call, Connection reset by peer

这个报错苦恼了我半天!后来通过一种方法完美解决了!

但是奇怪的是,android高版本手机就没问题,android低版本手机就会报错,不断复现!

1、异常信息

javax.net.ssl.SSLException: 

SSL handshake aborted: ssl=0x5f938320: I/O error during system call, Connection reset by peer


2、解决办法

//设置SSLContext 
		final SSLContext sslcontext = SSLContext.getInstance("TLS"); 
		sslcontext.init(null, new TrustManager[]{myX509TrustManager}, null);
		HttpsURLConnection.setDefaultSSLSocketFactory(sslcontext.getSocketFactory());
HttpsURLConnection urlCon = null;
		urlCon = (HttpsURLConnection) (new URL(urlString)).openConnection();
		urlCon.setReadTimeout(timeOut);
		urlCon.setConnectTimeout(timeOut);

再次编辑补充一下 myX509TrustManager:

private static TrustManager myX509TrustManager = new X509TrustManager() { 

	    @Override 
	    public X509Certificate[] getAcceptedIssuers() { 
	        return null; 
	    } 

	    @Override 
	    public void checkServerTrusted(final X509Certificate[] chain, final String authType) 
	    throws CertificateException { 
	    } 

	    @Override 
	    public void checkClientTrusted(final X509Certificate[] chain, final String authType) 
	    throws CertificateException { 
	    } 
	};


**不止出现在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; } ``` ```
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页