2016-01-26 20 views
5

Ho il mio retrofit impostato con HttpLoggingInterceptor come questo:Come stampare in Retrofit 2?

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

Sul mio esempio GSON, ho fatto setPrettyPrinting e ho ancora ottenere uscite JSON compatte. Ecco le mie librerie.

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' 

Come posso ottenere una bella stampa utilizzando Retrofit 2? Grazie.

EDIT: aggiornato il mio librerie e ancora non ha funzionato

risposta

0

Beta 2 non sempre rispetta le impostazioni GSON personalizzato. Prova l'aggiornamento a Beta 3.

Dal beta 3 change log -

Fix: Il convertitore GSON ora rispetta le impostazioni nell'istanza GSON (per esempio il serializeNulls). Ciò richiede Gson 2.4 o più recente.

Avrete anche bisogno di aggiornare a okhttp3 per beta3.

+0

provato con l'ultima beta4 e ancora non ha funzionato. – Schinizer

2

crea il tuo HttpLogginInterceptor personalizzato.

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

Nel tuo cliente, aggiungi:

OkHttpClient client = new OkHttpClient.Builder() 
      .addNetworkInterceptor(new CustomHttpLogging()) 
      .build(); 
1

Ispirato dalla risposta di Tanapruk Questo è quello che ho fatto per farlo funzionare con le mie versioni di retrofit (2.1.0) e okhttp.logging-intercettore (3.8.1). Ci scusiamo per il mix di Kotlin e Java.

Questa versione funziona per la stampa di oggetti e array JSON.

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 
     } 
    } 
} 

E nel client:

OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder(); 
HttpLoggingInterceptor httpLoggingInterceptor = 
    new HttpLoggingInterceptor(new ApiLogger()); 
httpLoggingInterceptor.setLevel(Level.BODY); 
httpClientBuilder.addInterceptor(httpLoggingInterceptor); 
Problemi correlati