使用 Retrofit 2 记录响应

Logging a Response using Retrofit 2

我需要记录我的服务器在调用后发送的响应,因为我收到了 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); 行中收到一个错误:"Retrofit 中的 client() cannot be applied to (com.squareup.okhttp.OkHttpClient)

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

谢谢。

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() ;