简体   繁体   中英

Logging a Response using Retrofit 2

I need to log the response my server is sending after a call, because I am receiving a MalformedJsonException, to see what is going on.

I am using this code:

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

I have done this following this tutorial. I get an error in ".client(client); line: "client() in Retrofit cannot be applied to (com.squareup.okhttp.OkHttpClient)

What I am doing wrong? What do I need to do to intercept the response from the server, to see what's wrong with the JSON?

Thank you.

client(OkHttpClient) is a method of Retrofit.Builder() not of Retrofit . Change

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

with

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM