【发布时间】:2016-06-01 07:31:27
【问题描述】:
我正在将我的代码移植到 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 的新 Apache HttpClient 包用于 Android,但这不符合我的目的。
我不能使用 Volley、Retrofit、OKHttp 等库,因为我有 Soap XML 格式的 Web 服务
我想完全脱离 apache。我关心的是知道 HttpsUrlConnection 类中的所有方法都可以用于替换。如果我能得到一个与我在上面所做的完全相同但使用新 API 方法的代码,我将不胜感激。我开始了解诸如 Registry、PoolingHttpClientConnectionManager 之类的类,但我不明白如何使用它们来获得上述结果
【问题讨论】:
-
使用 volley 或 retrofit 或 OkHttp 替换默认的 http 类。
-
@satnamsingh 感谢您的建议,但我更喜欢 HTTPUrlConnection,因为我所有的网络服务都在 SOAP xml 中
-
@HtooAungHlaing 我知道 apache legacy,但它现在似乎是一个临时解决方案,如此链接 github.com/androidquery/androidquery/issues/116 中给出的。还使用 apache legacy 导致我的应用程序多次崩溃
-
Marek Sebera 为 Android 开发的新 Apache HttpClient 包stackoverflow.com/a/32805048/2772552
标签: java android apache https android-6.0-marshmallow