How to pretty print in Retrofit 2?

I have my modification with HttpLoggingInterceptor as follows:

 Gson gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ") .setPrettyPrinting() // Pretty print .create(); HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(interceptor) .build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create(gson)) .client(client) .build(); 

In my Gson instance, I did setPrettyPrinting and I still get compact JSON outputs. Here are my libraries.

 compile 'com.google.code.gson:gson:2.5' compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4' compile 'com.squareup.okhttp3:logging-interceptor:3.0.1' compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4' compile 'com.squareup.okhttp3:okhttp:3.0.1' 

How can I get a pretty printed version using Retrofit 2? Thanks.

EDIT: Updated my libraries and still not working

+7
source share
4 answers

create your own HttpLogginInterceptor.

 public class CustomHttpLogging implements HttpLoggingInterceptor.Logger { @Override public void log(String message) { final String logName = "OkHttp"; if (!message.startsWith("{")) { Log.d(logName, message); return; } try { String prettyPrintJson = new GsonBuilder().setPrettyPrinting().create().toJson(new JsonParser().parse(message)); Log.d(logName, prettyPrintJson); } catch (JsonSyntaxException m) { Log.d(logName, message); } } } 

In your client add:

 OkHttpClient client = new OkHttpClient.Builder() .addNetworkInterceptor(new CustomHttpLogging()) .build(); 
+4
source

Inspired by Tanapruk's answer, this is what I did to get it working with my retrofit versions (2.1.0) and okhttp.logging-interceptor (3.8.1).

This version works for printing both objects and JSON arrays.

 class ApiLogger : HttpLoggingInterceptor.Logger { override fun log(message: String) { val logName = "ApiLogger" if (message.startsWith("{") || message.startsWith("[")) { try { val prettyPrintJson = GsonBuilder().setPrettyPrinting() .create().toJson(JsonParser().parse(message)) Log.d(logName, prettyPrintJson) } catch (m: JsonSyntaxException) { Log.d(logName, message) } } else { Log.d(logName, message) return } } } 

And in the client:

 val httpClientBuilder = OkHttpClient.Builder() val httpLoggingInterceptor = HttpLoggingInterceptor(ApiLogger()) httpLoggingInterceptor.level = Level.BODY httpClientBuilder.addInterceptor(httpLoggingInterceptor) 
+4
source

Beta 2 did not always respect gson user preferences. Try upgrading to beta 3.

From beta 3 change log -

Bugfix: the Gson converter now takes into account the settings on the supplied Gson instance (for example, serializeNulls). This requires Gson 2.4 or later.

You will also need to upgrade to okhttp3 for beta.

0
source

Thanks for the answer from Tanfruk Tangfianfang.

I improved it to support all Java Platform not only for Android .

Create PrettyLogger Class

 class PrettyLogger implements HttpLoggingInterceptor.Logger { private Gson mGson = new GsonBuilder().setPrettyPrinting().create(); private JsonParser mJsonParser = new JsonParser(); @Override public void log(String message) { String trimMessage = message.trim(); if ((trimMessage.startsWith("{") && trimMessage.endsWith("}")) || (trimMessage.startsWith("[") && trimMessage.endsWith("]"))) { try { String prettyJson = mGson.toJson(mJsonParser.parse(message)); Platform.get().log(INFO, prettyJson, null); } catch (Exception e) { Platform.get().log(WARN, message, e); } } else { Platform.get().log(INFO, message, null); } } } 

Use in Buidler:

 OkHttpClient.Builder builder = new OkHttpClient().newBuilder(); HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new PrettyLogger()); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); builder.addInterceptor(loggingInterceptor); 
0
source

Source: https://habr.com/ru/post/1241456/


All Articles