如何在Retrofit 2中打印漂亮?

Sch*_*zer 7 android gson retrofit2

我的改装设置HttpLoggingInterceptor如下:

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();
Run Code Online (Sandbox Code Playgroud)

在我的Gson实例上,我做了setPrettyPrinting,我仍然得到紧凑的JSON输出.这是我的图书馆.

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'
Run Code Online (Sandbox Code Playgroud)

如何使用Retrofit 2实现漂亮的打印?谢谢.

编辑:更新了我的库,仍然无法正常工作

And*_*æss 10

受到 Tanapruk 的回答的启发,这就是我为使其与我的改造版本 (2.1.0) 和 okhttp.logging-interceptor (3.8.1) 一起使用所做的工作。

此版本适用于打印 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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在客户端:

val httpClientBuilder = OkHttpClient.Builder()
val httpLoggingInterceptor = HttpLoggingInterceptor(ApiLogger())
httpLoggingInterceptor.level = Level.BODY
httpClientBuilder.addInterceptor(httpLoggingInterceptor)
Run Code Online (Sandbox Code Playgroud)

  • 如果 JSON 很长,它将被截断,您可以像这样逐行记录:“prettyPrintJson.lines().forEach { Log.d(logName, it) }” (3认同)

Tan*_*han 5

创建您自己的自定义 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在您的客户端中,添加:

OkHttpClient client = new OkHttpClient.Builder()
            .addNetworkInterceptor(new CustomHttpLogging())
            .build();
Run Code Online (Sandbox Code Playgroud)


iag*_*een 0

Beta 2 并不总是尊重自定义 gson 设置。尝试升级到 Beta 3。

来自beta 3 变更日志——

修复:Gson 转换器现在尊重所提供的 Gson 实例上的设置(例如serializeNulls)。这需要 Gson 2.4 或更高版本。

您还需要升级到 okhttp3 beta3。