HttpLoggingInterceptor

HttpLoggingInterceptor

这是我的拦截器

.addInterceptor(new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                @Override
                public void log(String message) {
                    LogUtil.d(message);
                }
            }).setLevel(HttpLoggingInterceptor.Level.BODY))


我的build.gridle是

compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'


当我发布api时,日志如下:

05-16 16:49:00.796 19039-19069/com.zdd.movie D/zddHttp: --> POST http://test.blhsc.com/api/resetPass.ashx http/1.1
05-16 16:49:00.796 19039-19069/com.zdd.movie D/zddHttp: Content-Type: application/json; charset=UTF-8
05-16 16:49:00.796 19039-19069/com.zdd.movie D/zddHttp: Content-Length: 183
05-16 16:49:00.797 19039-19069/com.zdd.movie D/zddHttp: {"systemVersion":"23","session":"","appVersion":"1","deviceTime":"1463388540788","deviceType":"2","account":"15980253327","deviceModel":"MI 4LTE","newPass":"123456","accountType":"0"}
05-16 16:49:00.797 19039-19069/com.zdd.movie D/zddHttp: --> END POST (183-byte body)
05-16 16:49:01.225 19039-19069/com.zdd.movie D/zddHttp: <-- 200 OK http://test.blhsc.com/api/resetPass.ashx (428ms)
05-16 16:49:01.225 19039-19069/com.zdd.movie D/zddHttp: Cache-Control: private
05-16 16:49:01.225 19039-19069/com.zdd.movie D/zddHttp: Content-Length: 73
05-16 16:49:01.225 19039-19069/com.zdd.movie D/zddHttp: Content-Type: application/json;charset=UTF-8
05-16 16:49:01.225 19039-19069/com.zdd.movie D/zddHttp: Server: Microsoft-IIS/7.5
05-16 16:49:01.225 19039-19069/com.zdd.movie D/zddHttp: X-AspNet-Version: 4.0.30319
05-16 16:49:01.225 19039-19069/com.zdd.movie D/zddHttp: X-Powered-By: ASP.NET
05-16 16:49:01.225 19039-19069/com.zdd.movie D/zddHttp: Date: Mon, 16 May 2016 08:49:03 GMT
05-16 16:49:01.225 19039-19069/com.zdd.movie D/zddHttp: OkHttp-Sent-Millis: 1463388540799
05-16 16:49:01.225 19039-19069/com.zdd.movie D/zddHttp: OkHttp-Received-Millis: 1463388541225
                                                    }   --> this line is incorrect
05-16 16:49:01.226 19039-19069/com.zdd.movie D/zddHttp: <-- END HTTP (73-byte body)


问题与Retrofit 2 prints empty response body in log with HttpLoggingInterceptor.Level.BODY相同

这个问题让我厌倦了一个星期。我现在疯了。请帮助我。仍然谢谢您。

最佳答案

尝试像这样开发您的拦截器:

public class AppUtil {
public static final boolean isEnableLogging = true;
public static Retrofit getRetrofitInstance() {

        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        if(isEnableLogging)
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        else
            logging.setLevel(HttpLoggingInterceptor.Level.NONE);
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(logging);

        return new Retrofit.Builder()
                .baseUrl(Constants.URL_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient.build())
                .build();
    }
}

关于android - retrofit2 HttpLoggingInterceptor响应正文日志不正确,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37250126/

10-11 20:10