已弃用的org.apache.http.conn.scheme.SchemeRegistry、ThreadSafeClientConnManager类的替代方案等

3

我正在将我的代码移植到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等类,但我不知道如何使用它们来实现上述结果。


使用Volley、Retrofit或OkHttp替换默认的HTTP类。 - user1682446
@satnamsingh 谢谢您的建议,但我更喜欢使用HTTPUrlConnection,因为我的所有Web服务都是基于SOAP XML的。 - Dhiraj Sharma
请阅读https://dev59.com/CF0a5IYBdhLWcg3wPWlN,这可能是重复的问题。 - Htoo Aung Hlaing
@HtooAungHlaing 我知道关于apache legacy的事情,但它似乎是一个暂时的解决方案,就像在这个链接https://github.com/androidquery/androidquery/issues/116中所提到的一样。另外,使用apache legacy会导致我的应用程序在多种情况下崩溃。 - Dhiraj Sharma
1
Marek Sebera为Android开发的全新Apache HttpClient包 https://dev59.com/CF0a5IYBdhLWcg3wPWlN#32805048 - Htoo Aung Hlaing
@HtooAungHlaing 哦,我很感激,但那不是我想要的。我想完全迁移离开 Apache。我的关注点是知道 HttpsUrlConnection 类中的所有方法都可以用于替换哪些方法。如果我可以得到一个代码,它可以使用新的 API 方法来完全执行我上面所做的操作,我将不胜感激。我了解到 Registry、PoolingHttpClientConnectionManager 等类,但我无法理解如何使用它们来实现上述结果。 - Dhiraj Sharma
1个回答

0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接