当前位置: 首页 > 知识库问答 >
问题:

改装2.x:请求和响应的日志头

梁德馨
2023-03-14

我正在使用改造2.x,我想记录请求和响应的标头和正文。

  HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(interceptor)
            .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
            .addNetworkInterceptor(new Interceptor() {
                @Override
                public okhttp3.Response intercept(Chain chain) throws IOException {
                    Request request = chain.request().newBuilder()
                            .addHeader("key", "value")
                            .addHeader("HEADER","HEADER Value")
                            .build();
                    return chain.proceed(request);
                }


            }).build();

这是我正在做的,我的问题是请求的标题没有被记录在Android Monitor中,但其余的一切都被记录了。

Gradle版本

 compile ('com.squareup.retrofit2:retrofit:2.0.0-beta3') {
    // exclude Retrofit’s OkHttp peer-dependency module and define your own module import
    exclude module: 'okhttp'
}
compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta3'
compile ('com.squareup.okhttp3:logging-interceptor:3.0.1'){
    exclude module: 'okhttp'
}

使用RC1和3.0.1由于错误问题报告错误链接

共有3个答案

甄正信
2023-03-14

愿它帮助某人...

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

添加两者以查看完整的日志,并在最后添加这个拦截器(不知道为什么,但就像这样)。

公羊信厚
2023-03-14

不是使用addInterceptor添加日志拦截器,而是使用addNetworkInterceptor,以包含OkHttp添加的标头。

网络拦截器能够:

观察数据,就像通过网络传输数据一样。

籍永安
2023-03-14

哦,如果有人感兴趣,我发现了错误:

 HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
        .addInterceptor(interceptor)
        .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
        .addInterceptor(new Interceptor() {
            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                Request request = chain.request().newBuilder()
                        .addHeader("key", "value")
                        .addHeader("HEADER","HEADER Value")
                        .build();
                return chain.proceed(request);
            }


        }).build();

您必须在请求拦截器之后添加日志拦截器(您的拦截器变量),因此正确答案是:

 HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
            @Override
            public okhttp3.Response intercept(Chain chain) throws 
 IOException {
                Request request = chain.request().newBuilder()
                        .addHeader("key", "value")
                        .addHeader("HEADER","HEADER Value")
                        .build();
                return chain.proceed(request);
            }


        })
        .addInterceptor(interceptor)
        .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
        .build();
 类似资料:
  • API接口 在我的MVP节目中 这些是我的改造和rxjava代码,我应该发布一封电子邮件和密码来注册用户。服务器应该在成功时返回一个字符串,在失败时也返回一个字符串。 我添加了gson setLenient代码部分,因为它给了我错误,如果我没有它。添加此后,我得到了错误,我不知道如何解决它。这是因为来自服务器的返回响应是一个字符串吗? 提前谢谢。

  • 我正试图通过改型2获得原始响应。0.2. 到目前为止,我试图使用以下代码行打印响应,但它打印的是地址,而不是确切的响应正文。 日志i(“原始消息”,response.body()。toString());

  • 上一个小节中,我们简单的介绍了 HTTP 协议,但是,并没有针对 HTTP 的请求和响应进行更详尽的描述。但是,分析请求和响应信息是我们进行爬虫工作中的重要步骤,因此,有必要详细的介绍这两个步骤。 我们还是复用之前的访问慕课网的例子进行 HTTP 协议的解析。关于怎么获取请求头和响应头的信息的内容,我们会在后面讲解第一个爬虫的时候进行讲解。 使用 get 方法请求慕课网的请求信息如下: GET /

  • HTTP请求和HTTP响应在任何Web应用程序中都发挥着重要作用。 我们需要获取http请求的完整详细信息以便正确处理它。 处理完毕后,我们需要通过http响应将处理后的数据发送给客户端。 FuelPHP提供了出色的Request和Response类,分别用于读写HTTP请求和HTTP响应。 让我们在本章中了解Request和Response类。 Request 在典型的Web应用程序中,应用程序