简体   繁体   English

使用Retrofit获取原始HTTP响应

[英]Get raw HTTP response with Retrofit

I want to get the raw http response from my API REST. 我想从我的API REST获取原始http响应。 I have tried with this interface: 我试过这个界面:

@POST("/login")
@FormUrlEncoded
Call<retrofit.Response> login(@Field("username") String login, @Field("password") String pass,
                     @Field("appName") String appName, @Field("appKey") String appKey);

But I get: 但我得到:

java.lang.IllegalArgumentException: Unable to create call adapter for retrofit.Call for method Api.login java.lang.IllegalArgumentException:无法为方法Api.login的retrofit.Call创建调用适配器

I create Retrofit this way: 我用这种方式创建Retrofit

Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
retrofitBuilder.addConverterFactory(JacksonConverterFactory.create());
Retrofit retrofitAdapter = retrofitBuilder.baseUrl(baseUrl).build();
return retrofitAdapter.create(apiClass);

To get access to the raw response, use ResponseBody from okhttp as your call type. 要访问原始响应,请使用okhttp中的ResponseBody作为您的调用类型。

Call<ResponseBody> login(...)

In your callback, you can check the response code with the code method of the response. 在回调中,您可以使用响应的code方法检查响应代码。 This applies to any retrofit 2 return type, because your callback always gets a Response parameterized with your actual return type. 这适用于任何改进2返回类型,因为您的回调总是获得一个使用您的实际返回类型参数化的Response For asynchronous -- 对于异步 -

Call<ResponseBody> myCall = myApi.login(...)
myCall.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Response<ResponseBody> response, Retrofit retrofit) {
        // access response code with response.code()
        // access string of the response with response.body().string()
    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
});

for synchronous calls -- 用于同步通话 -

Response<ResponseBody> response = myCall.execute();
System.out.println("response code" + response.code());

You can get information about headers, response code, down to raw json response body by using Interceptors . 您可以使用拦截器获取有关标头,响应代码,原始json响应体的信息。 You can write your custom Interceptors but I prefer to use Square's own Logging Interceptor. 您可以编写自定义拦截器,但我更喜欢使用Square自己的Logging Interceptor。 It's available on maven central. 它可以在maven中心使用。

compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'

Here's how to use it 这是如何使用它

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(interceptor).build();

Logging level BODY will print headers to the body response. 记录级别BODY将标题打印到正文响应。 And in your Retrofit 在你的改造中

Retrofit retrofit = new Retrofit.Builder()
            .client(client)               
            .baseUrl("https://yourapi.com/api/")
            .build();

Now, open up Log Cat and you'll see raw HTTP response. 现在,打开Log Cat,您将看到原始HTTP响应。

Caution! 警告!

Don't forget to remove Interceptors (or change Logging Level to NONE) in production! 不要忘记在生产中删除拦截器(或将记录级别更改为NONE)! Otherwise people will be able to see your request and response on Log Cat. 否则,人们将能够在Log Cat上看到您的请求和响应。

I had the same problem but a different solution. 我有同样的问题,但有一个不同的解决方案。 I took the request and send it with OkHttp3Client. 我接受了请求并使用OkHttp3Client发送它。 Like this: 像这样:

//raw text 
Request request = call.clone().request();
OkHttpClient client = new OkHttpClient();
okhttp3.Response test = client.newCall(request).execute();
System.out.println(test.body().string());

source: http://robinhenniges.com/de/retrofit-2-raw-response 来源: http//robinhenniges.com/de/retrofit-2-raw-response

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM