【问题标题】:Client (com.squareup.okhttp.OkhttpClient) in builder cannot be applied to (okhttp3.OkHttpClient)构建器中的客户端(com.squareup.okhttp.OkhttpClient)无法应用于(okhttp3.OkHttpClient)
【发布时间】:2017-03-08 17:16:34
【问题描述】:

我正在尝试使用 Retrofit2 并在 Full 中记录错误,就像在 Retrofit1 中使用

.setLogLevel(RestAdapter.LogLevel.FULL)

我在使用 .client(Okhttp 客户端)时遇到错误;这是我的依赖项:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-beta5'
    testCompile 'junit:junit:4.12'

    compile 'com.google.code.gson:gson:2.4'
    compile 'com.squareup.retrofit:retrofit:2.0.0-beta3'
    compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'

    compile 'com.squareup.okhttp3:okhttp:3.2.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'

}

这是我的java代码:

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Api.BASE_URL)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    Interface service = retrofit.create(Interface.class);

我正在使用 Retrofit 2.0.0-beta3 并且仍然遇到此问题。我不能从这里继续前进。任何帮助表示赞赏。见下图:

这是我下面的导入语句:

【问题讨论】:

  • 使用稳定版改造

标签: android retrofit okhttp okhttp3


【解决方案1】:

看来您的导入语句来自okhttp, 检查您的导入:-

必须是import okhttp3.OkHttpClient;

或者你的改造导入一定是错误的..它必须是retrofit2.Retrofit

[编辑]

他们的 beta 版本似乎有问题,将其恢复为稳定版本。

【讨论】:

  • 当然,我已经检查过了。它导入 okhttp3.OkHttpClient;
  • 好吧,但你的改造似乎是retrofit.Retrofit,必须是retrofit2.Retrofit
  • 可能是测试版的问题
  • 谢谢,这个“retrofit2.Retrofit”解决了它。我导入了错误的东西:(
  • 哈哈哈......我无缘无故责怪改造人。
【解决方案2】:

Retrofit 暂时不再处于测试阶段。请改用com.squareup.retrofit2:retrofit:2.2.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-17
    • 2018-03-07
    • 2017-03-06
    • 2012-09-11
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    相关资源
    最近更新 更多