类'org.apache.http.conn.ssl.SSLSocketFactory'没有实现接口。

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

在我的安卓应用中:appbuild.gradle。

  android {
        compileSdkVersion 28
        defaultConfig {
            minSdkVersion 18
            targetSdkVersion 28
            versionCode 427
            versionName "2.1.427"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

dependencies {
    annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
    annotationProcessor "org.androidannotations:ormlite:$AAVersion"

    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:animated-vector-drawable:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.android.support:customtabs:28.0.0'
    implementation 'com.android.support:exifinterface:28.0.0'
    implementation 'com.android.support:support-media-compat:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'

    implementation 'com.crashlytics.sdk.android:crashlytics:2.9.7'
    implementation 'com.google.android.gms:play-services-gcm:16.0.0'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.j256.ormlite:ormlite-android:5.1'
    implementation 'commons-codec:commons-codec:1.11'
    implementation 'commons-io:commons-io:2.6'
    implementation 'org.apache.commons:commons-lang3:3.8.1'
    implementation 'org.apache.httpcomponents:httpclient:4.3.6'
    implementation "org.androidannotations:androidannotations-api:$AAVersion"
    implementation "org.androidannotations:ormlite-api:$AAVersion"

    implementation project(':fab')
    implementation project(':fointeraction')
    implementation project(':scanlib')

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
}

我用"org.apache.http"来创建http请求。

当我在Android 6.0上启动我的应用程序时,它成功启动。而且我成功地创建了http请求,并成功地得到http响应,很好。

url = https://some_host/

但当我在Android 9.0上启动我的应用时,我成功创建http请求,但没有返回http响应。这里有错误。

Class 'org.apache.http.conn.ssl.SSLSocketFactory' does not implement interface 'org.apache.http.conn.scheme.SchemeSocketFactory' in call to 'java.net.Socket org.apache.http.conn.scheme.SchemeSocketFactory.createSocket(org.apache.http.params.HttpParams)' (declaration of 'org.apache.http.impl.conn.DefaultClientConnectionOperator' appears in /data/app/com.myproject.app-fa20LoyoX_ze9Vk1tE46kg==/split_lib_dependencies_apk.apk)
 java.lang.IncompatibleClassChangeError: Class 'org.apache.http.conn.ssl.SSLSocketFactory' does not implement interface 'org.apache.http.conn.scheme.SchemeSocketFactory' in call to 'java.net.Socket org.apache.http.conn.scheme.SchemeSocketFactory.createSocket(org.apache.http.params.HttpParams)' (declaration of 'org.apache.http.impl.conn.DefaultClientConnectionOperator' appears in /data/app/com.myproject.app-fa20LoyoX_ze9Vk1tE46kg==/split_lib_dependencies_apk.apk)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:165)
    at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446)
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
    at com.myproject.app.transport.lite.FOServiceLite.executeRequestLow(FOServiceLite.java:322)
    at com.myproject.app.transport.lite.FOServiceLite.loadApplicationProfileResponse(FOServiceLite.java:479)
    at com.myproject.app.transport.lite.FOServiceLite.executeCommand(FOServiceLite.java:494)
    at com.myproject.app.transport.lite.FOServiceLite.executeCommand(FOServiceLite.java:679)
    at com.myproject.app.service.ProviderService.sync(ProviderService.java:53)
    at com.myproject.app.fragment.SubscribesFragment.loadProviders(SubscribesFragment.java:73)
    at com.myproject.app.fragment.SubscribesFragment_.access$1801(SubscribesFragment_.java:23)
    at com.myproject.app.fragment.SubscribesFragment_$19.execute(SubscribesFragment_.java:291)
    at org.androidannotations.api.BackgroundExecutor$Task.run(BackgroundExecutor.java:405)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:459)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:764)

这里的java片段:

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
    HttpPost post = new HttpPost(url.toString());
                    post.setEntity(be);
                    be = null;
                    // Reporting purpose
                    requestStartDate = new Date();
                    HttpResponse httpResponse = client.execute(post);
                    post = null;
                    StatusLine statusLine = httpResponse.getStatusLine();

这一行出现错误。

HttpResponse httpResponse = client.execute(post);
android apache
1个回答
1
投票

这个解决方案已经在 这个github评论 来自Tijo Thomas于2019年3月4日.后来在评论中也报告了同样的修复方法。此处.今天,我可以通过在Android Studio中调试我的类似应用来确认,下面的代码可以做到这一点。

//Important for android version 9 pie/
client.getConnectionManager().getSchemeRegistry().register(
        new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)
);
HttpResponse response = client.execute(httpGet);

当然,当你在Android Studio中复制上面的行数时,它会建议相应的导入(如果还没有的话)。

import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;

现在,为了使我的回答具有充分的普遍性,我想补充一点,例如,在我的 微博应用我还必须修复 OAuth 供应商,那是 隐性 使用一个默认的http客户端.所以,为了有利于其他开发人员,考虑到我搜索也在我的应用程序的进口,我也包括指示为。

CommonsHttpOAuthProvider httpOauthprovider = new CommonsHttpOAuthProvider("https://api.twitter.com/oauth/request_token","https://api.twitter.com/oauth/access_token", "https://api.twitter.com/oauth/authorize");
HttpClient httpClient = new DefaultHttpClient();
//Important for android version 9 pie/
httpClient.getConnectionManager().getSchemeRegistry().register(
        new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)
);
httpOauthprovider.setHttpClient(httpClient); // notice that the oauth provider was implicitly using the http client before!
© www.soinside.com 2019 - 2024. All rights reserved.