H.fate
H.fate

Reputation: 644

How to get json log using retrofit2 in Android studio?

I read https://futurestud.io/blog/retrofit-2-log-requests-and-responses and made it.

    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);

    OkHttpClient client = new OkHttpClient();
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.interceptors().add(logging);

    client = builder.build();

    Retrofit retrofit = new Retrofit.Builder()
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(LoginApiService.Login_API_URL)
            .build();

But it's returned result like

09-08 05:12:24.219 25389-26089/com.example.keepair.myapplication D/OkHttp: --> POST http://ec2-52-78-138-143.ap-northeast-2.compute.amazonaws.com:8000/rest-auth/login/ http/1.1
09-08 05:12:24.219 25389-26089/com.example.keepair.myapplication D/OkHttp: Content-Type: application/json; charset=UTF-8
09-08 05:12:24.219 25389-26089/com.example.keepair.myapplication D/OkHttp: Content-Length: 46
09-08 05:12:24.219 25389-26089/com.example.keepair.myapplication D/OkHttp: --> END POST
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: <-- 400 Bad Request http://ec2-52-78-138-143.ap-northeast-2.compute.amazonaws.com:8000/rest-auth/login/ (96ms)
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: Date: Wed, 07 Sep 2016 20:11:27 GMT
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: Server: WSGIServer/0.2 CPython/3.4.3
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: Vary: Accept
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: Allow: POST, OPTIONS
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: X-Frame-Options: SAMEORIGIN
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: Content-Type: application/json
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: <-- END HTTP

It's not enough.

Because i'm using RESTful api and json type,

  1. I want check json request that i send to api and json response that comes from api. For example, { "hi" : "bye" }, full information of json request and response.
  2. Additionally i want to know exactly what has send through header from my application.

What should i do?

Upvotes: 1

Views: 82

Answers (1)

Nitzan Tomer
Nitzan Tomer

Reputation: 164147

If you take a look at the code of HttpLoggingInterceptor.setLevel:

public HttpLoggingInterceptor setLevel(Level level) {
    if (level == null) throw new NullPointerException("level == null. Use Level.NONE instead.");
    this.level = level;
    return this;
}

You'll see that every time you are calling this method you are overriding the last call, so doing this:

logging.setLevel(HttpLoggingInterceptor.Level.BODY);
logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);

Sets the level to Level.HEADERS.
Which is exactly what's happening in your logs, you see only the headers.
Just remove that (2nd) line and you'll have Level.BODY which is what you're looking for.

Upvotes: 1

Related Questions