【问题标题】:Replacement for deprecated org.apache.http.conn.scheme.SchemeRegistry, ThreadSafeClientConnManager class etc替换已弃用的 org.apache.http.conn.scheme.SchemeRegistry、ThreadSafeClientConnManager 类等
【发布时间】: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 之类的类,但我不明白如何使用它们来获得上述结果

【问题讨论】:

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


【解决方案1】:

Apache HTTP 客户端弃用

根据 Google 的说法,在 Android 6.0 中,他们删除了对 Apache HTTP 客户端的支持。从 Android 9 开始,该库已从引导类路径中移除,并且默认情况下不适用于应用。

因此,如果您仍希望将其提供给您的应用程序,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

【讨论】:

    猜你喜欢
    • 2016-12-09
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多