【问题标题】:Logging a Response using Retrofit 2使用 Retrofit 2 记录响应
【发布时间】:2015-11-12 10:29:55
【问题描述】:

我需要记录我的服务器在调用后发送的响应,因为我收到了 MalformedJsonException,以查看发生了什么。

我正在使用此代码:

OkHttpClient client = new OkHttpClient();
        client.interceptors().add(new Interceptor() {

            @Override
            public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
                com.squareup.okhttp.Response respuesta = chain.proceed(chain.request());
                Log.i("David", "Response: "+respuesta.toString());

                return response;
            }
        });
        Retrofit builder = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .client(client);

我按照this 教程完成了这个。我在 ".client(client); line: "client() in Retrofit cannot be applied to (com.squareup.okhttp.OkHttpClient) 中收到错误

我做错了什么?我需要做什么来拦截来自服务器的响应,看看 JSON 有什么问题?

谢谢。

【问题讨论】:

    标签: android retrofit


    【解决方案1】:

    client(OkHttpClient)Retrofit.Builder() 的方法,而不是 Retrofit 的方法。改变

    Retrofit builder = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .client(client);
    

    Retrofit builder = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build() ;
    

    【讨论】:

    • 错误消失了。谢谢!我可以在十分钟内接受你的回答。
    猜你喜欢
    • 1970-01-01
    • 2014-03-20
    • 2018-01-15
    • 2016-01-18
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    相关资源
    最近更新 更多