ClassCastException:ch.boye.httpclientandroidlib.impl.client.InternalHttpClient无法强制转换为org.apache.http.client.HttpClient

时间:2014-11-01 07:59:13

标签: android retrofit apache-commons-httpclient httpclientandroidlib

我需要使用自定义HttpClient实例化retrofit apache client

我在没有任何问题的Android项目中运行它,但是当我在我的Android项目中移植它时,我遇到了Unable to resolve static field的问题...经过一些研究我发现 this answer说服我尝试将httpclientandroidlib集成到我的代码中。

现在的问题在于ClassCastException: ch.boye.httpclientandroidlib.impl.client.InternalHttpClient cannot be cast to org.apache.http.client.HttpClient 在这一行:

    .setClient(new ApacheClient((HttpClient)UnsafeHttpsClient.createUnsafeClient())) // (new ApacheClient(UnsafeHttpsClient.createUnsafeClient())) // FR Qui usa Retrifit
mSymtService = new SecuredRestBuilder()
    .setLoginEndpoint(TEST_URL + VideoSvcApi.TOKEN_PATH)
    .setClient(new ApacheClient((HttpClient)UnsafeHttpsClient.createUnsafeClient())) // (new ApacheClient(UnsafeHttpsClient.createUnsafeClient())) // FR Qui usa Retrifit
    .setEndpoint(TEST_URL).setLogLevel(LogLevel.FULL).build()
    .create(SymtMngmSrvcApi.class);

LogCat报道: 11-01 07:17:07.723: E/AndroidRuntime(2019): java.lang.ClassCastException: ch.boye.httpclientandroidlib.impl.client.InternalHttpClient cannot be cast to org.apache.http.client.HttpClient 11-01 07:17:07.723: E/AndroidRuntime(2019): at ![org.coursera.symtmngm.SymtMngmBaseActivity.signIn][1](SymtMngmBaseActivity.java:132) 11-01 07:17:07.723: E/AndroidRuntime(2019): at org.coursera.symtmngm.LoginActivity$1.onClick(LoginActivity.java:75) 这是返回我的自定义HttpClient的方法:

    public static HttpClient createUnsafeClient() {
    try {
        ch.boye.httpclientandroidlib.conn.ssl.SSLContextBuilder builder = new ch.boye.httpclientandroidlib.conn.ssl.SSLContextBuilder();
        builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                builder.build());
        CloseableHttpClient httpclient = HttpClients.custom()
                .setSSLSocketFactory(sslsf).build();

        return httpclient;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

这是我的课程路径: enter image description here

更新:已解决

我通过使用解决了 httpclient-android-4.3.5.jar代替httpclientandroidlib

0 个答案:

没有答案