android - 使用 Retrofit2 的 HTTP 请求得到 "CLEARTEXT communication not supported"错误

标签 android android-studio retrofit retrofit2

我的 Api 接口(interface)中有一个请求:

@FormUrlEncoded
@POST(ApiStatics.authorizeURL)
Call<Oauth2Model> authorizer(@Field("grant_type") String grantType,
                             @Field("username") String userName,
                             @Field("password") String password,
                             @Field("client_id") String clientID,
                             @Field("client_secret") String clientSecret);

这个 getApi 方法:

public static MyAPI getApi() {
    if (api == null) {
        OkHttpClient client;
        if (GuildsApp.isDebug()) {
            HttpLoggingInterceptor bodyInterceptor = new HttpLoggingInterceptor();
            bodyInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            HttpLoggingInterceptor headerInterceptor = new HttpLoggingInterceptor();
            headerInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
            client = new OkHttpClient.Builder().readTimeout(5, TimeUnit.MINUTES).writeTimeout(5, TimeUnit.MINUTES).connectTimeout(5, TimeUnit.MINUTES).addInterceptor(bodyInterceptor).addInterceptor(headerInterceptor).build();
        } else {
            client = new OkHttpClient.Builder().readTimeout(5, TimeUnit.MINUTES).writeTimeout(5, TimeUnit.MINUTES).connectTimeout(5, TimeUnit.MINUTES).build();
        }
        Retrofit.Builder builder = new Retrofit.Builder();
        builder.baseUrl(BASE_URL);
        builder.addConverterFactory(GsonConverterFactory.create());
        builder.client(client);
        Retrofit retrofit = builder.build();
        api = retrofit.create(MyAPI.class);
    }
    return api;
}

我通过以下方法在我的 fragment 中使用此 api:

private void login(String user, String pass) {
    ApiManager.getApi().authorizer("password", user, pass, ApiStatics.CID,ApiStatics.CSecret).enqueue(new Callback<Oauth2Model>() {
        @Override
        public void onResponse(Call<Oauth2Model> call, Response<Oauth2Model> response) {
            Log.e("authorize token",""+response.body().access_token);
        }

        @Override
        public void onFailure(Call<Oauth2Model> call, Throwable t) {
            Log.e("Failure ",""+t.getLocalizedMessage());
        }
    });
}

当我运行项目时,请求使用以下日志调用 onFailure 方法:

E/Failure: CLEARTEXT communication not supported: [ConnectionSpec(cipherSuites=[TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, TLS_DHE_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_RSA_WITH_AES_256_CBC_SHA, TLS_RSA_WITH_AES_128_GCM_SHA256, TLS_RSA_WITH_AES_128_CBC_SHA, TLS_RSA_WITH_AES_256_CBC_SHA, TLS_RSA_WITH_3DES_EDE_CBC_SHA], tlsVersions=[TLS_1_2, TLS_1_1, TLS_1_0], supportsTlsExtensions=true), ConnectionSpec(cipherSuites=[TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, TLS_DHE_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_RSA_WITH_AES_256_CBC_SHA, TLS_RSA_WITH_AES_128_GCM_SHA256, TLS_RSA_WITH_AES_128_CBC_SHA, TLS_RSA_WITH_AES_256_CBC_SHA, TLS_RSA_WITH_3DES_EDE_CBC_SHA], tlsVersions=[TLS_1_0], supportsTlsExtensions=true)]

我的 BASE_URL 是 HTTP ,而不是 HTTPS。 我的登录方法用 login("",""); 调用服务器接受“”, 我该如何解决这个错误? 我使用Retrofit 2.1.0,我的测试设备是nexus 5x android N

最佳答案

将此注释添加到类的顶部。

@PowerMockIgnore("javax.net.ssl.*")

它将忽略 SSL 检查。

关于android - 使用 Retrofit2 的 HTTP 请求得到 "CLEARTEXT communication not supported"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38411660/

相关文章:

android - Android 应用程序中的验证码

Android - 相当于 UITableViewCell?

java - 如何避免 java.lang.OutOfMemoryError : bitmap size exceeds VM budget in android while using a large bitmap?

java - Android 应用无法再访问服务器文件

java - 无法解析 R android studio 1.2.1.1

java - ViewPager 仅在网络调用成功后加载

java - 线程在同步块(synchronized block)中停止

performance - Access denied find property "persist.vendor.camera.privapp.list"这个问题怎么解决?

retrofit - 仅当前面的可观察成功时才调用服务

android - 自定义改造 ErrorHandler 给出 UndeclaredThrowableException