我正在将我的代码移植到Android 6.0。由于Apache类已经在API 23中被弃用和删除,所以我无法找到与我的先前代码完全匹配的内容来建立HTTPS连接。 以下是我的代码:
try {
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https",new CustomSSLSocketFactory(), 443));// new CustomSSLSocketFactory().newSslSocketFactory(context.getResources()), 443));
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 120000);
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParameters, schemeRegistry);
httpClient = new DefaultHttpClient(cm, httpParameters);
setHttpClient(httpClient);
AbstractMediator.setServerTime(System.currentTimeMillis());
httpResponse = httpClient.execute(request);
serviceResponseObject.setResponseCode(httpResponse.getStatusLine().getStatusCode());
serviceResponseObject.setMessage(httpResponse.getStatusLine().getReasonPhrase());
serviceResponseObject.setAllHeader(httpResponse.getAllHeaders());
Header[] header = httpResponse.getAllHeaders();
}catch (UnknownHostException e){
Utility.printStackTrace(e);
serviceResponseObject.setResponseBody("");
return responseWithErrorCode(NETWORK_ERROR_CODE101, serviceResponseObject);
} catch (IOException e) {
Utility.printStackTrace(e);
return responseWithErrorCode(NETWORK_ERROR_CODE100, serviceResponseObject);
} catch (Exception e) {
Utility.printStackTrace(e);
return responseWithErrorCode(NETWORK_ERROR_CODE100, serviceResponseObject);
}
我已经阅读了这个链接 Deprecated: org.apache.http.conn.scheme.scheme is deprecated。它提到了构造函数的替换方法,但没有提到其他方法。我的应用程序最低API级别为14,如果有任何替代方法可以支持它,我会非常高兴。
我阅读了这个链接 'org.apache.http.HttpEntity' is deprecated. How to solve this error in android studio? 它提到了使用apache legacy和Marek Sebera's新的Apache HttpClient包来解决此问题,但这不符合我的需求。
由于我使用的是Soap XML格式的Web服务,因此我无法使用Volley,Retrofit,OKHttp等库。
我想完全迁移离开apache。我的关注点在于了解HttpsUrlConnection类中可以用作替换的方法。如果我能得到一个关于如何使用新的API方法完成上述操作的代码,我将不胜感激。我了解了Registry和PoolingHttpClientConnectionManager等类,但我不知道如何使用它们来实现上述结果。