取代已弃用的org.apache.http.conn.scheme.SchemeRegistry,ThreadSafeClientConnManager类等

问题描述 投票:3回答:1

我正在将代码移植到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遗留工具以及有关Marek Sebera的Android新Apache HttpClient软件包的其他解决方案,但这不符合我的目的。

我无法使用Volley,Retrofit,OKHttp之类的库,因为我具有Soap XML格式的Web服务

我想完全从Apache迁移过来。我的担心是要知道HttpsUrlConnection类中的所有方法都可以用于替换。如果我能得到与此相关的代码,并且使用新的API方法,那么我将不胜感激。我开始了解诸如Registry,PoolingHttpClientConnectionManager之类的类,但我不明白如何使用它们来获得上述结果

java android apache https android-6.0-marshmallow
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.