已弃用的org.apache.http.conn.scheme.SchemeRegistry,ThreadSafeClientConnManager类等的替换

时间:2016-06-01 07:31:27

标签: java android apache https android-6.0-marshmallow

我将我的代码移植到android 6.0。由于API API中已经弃用并删除了apache类,因此我无法找到与之前代码完全匹配的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。它确实提到了scheme类的新构造函数,但没有提到其他方法。我的app min API级别是14,如果任何替代方法都尊重它,我会很高兴。

我已经阅读了这篇'org.apache.http.HttpEntity' is deprecated. How to solve this error in android studio?,其中讨论了使用apache遗留问题以及关于Marek Sebera用于Android的新Apache HttpClient软件包的另一个解决方案,但这不符合我的目的。

我不能使用像Volley,Retrofit,OKHttp这样的库,因为我有Soap XML格式的Web服务

我想完全从apache迁移出去。我关心的是知道HttpsUrlConnection类中的所有方法都可以用于替换。我将不胜感激,如果我得到一个关于这个的代码,它正是我正在做的,但是使用新的API方法。我开始了解像Registry,PoolingHttpClientConnectionManager这样的类,但我不明白如何使用它们来获得上述结果

1 个答案:

答案 0 :(得分:0)

Apache HTTP客户端弃用

据Google称,在Android 6.0中,他们删除了对Apache HTTP客户端的支持。从Android 9开始,该库已从bootclasspath中删除,并且默认情况下不适用于应用程序。

因此,如果您仍然希望将其提供给您的应用程序,则Google也提供了解决方案。

AndroidManifest.xml文件的应用程序内部使用以下标记

<uses-library android:name="org.apache.http.legacy" android:required="false"/>

有关迁移过程的更多详细信息,请参阅Android文档。 https://developer.android.com/about/versions/pie/android-9.0-changes-28#apache-p