Java 使用 Retrofit 获取原始 HTTP 响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33282889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get raw HTTP response with Retrofit
提问by Héctor
I want to get the raw http response from my API REST. I have tried with this interface:
我想从我的 API REST 获取原始 http 响应。我试过这个界面:
@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
I create Retrofit
this way:
我这样创建Retrofit
:
Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
retrofitBuilder.addConverterFactory(HymansonConverterFactory.create());
Retrofit retrofitAdapter = retrofitBuilder.baseUrl(baseUrl).build();
return retrofitAdapter.create(apiClass);
采纳答案by iagreen
To get access to the raw response, use ResponseBody
from okhttp as your call type.
要访问原始响应,请使用ResponseBody
from okhttp 作为您的调用类型。
Call<ResponseBody> login(...)
In your callback, you can check the response code with the code
method of the response. This applies to any retrofit 2 return type, because your callback always gets a Response
parameterized with your actual return type. For asynchronous --
在您的回调中,您可以使用响应的code
方法检查响应代码。这适用于任何改造 2 返回类型,因为您的回调始终Response
使用您的实际返回类型进行参数化。对于异步——
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());
回答by aldok
You can get information about headers, response code, down to raw json response body by using Interceptors. You can write your custom Interceptors but I prefer to use Square's own Logging Interceptor. It's available on maven central.
您可以使用Interceptors获取有关标头、响应代码乃至原始 json 响应正文的信息。您可以编写自定义拦截器,但我更喜欢使用 Square 自己的日志拦截器。它在 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. And in your Retrofit
日志级别 BODY 将标题打印到正文响应。在你的改造中
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! Otherwise people will be able to see your request and response on Log Cat.
不要忘记在生产中删除拦截器(或将日志级别更改为无)!否则人们将能够在 Log Cat 上看到您的请求和响应。
回答by Rob Anderson
I had the same problem but a different solution. I took the request and send it with OkHttp3Client. Like this:
我有同样的问题,但有不同的解决方案。我接受了请求并使用 OkHttp3Client 发送它。像这样:
//raw text
Request request = call.clone().request();
OkHttpClient client = new OkHttpClient();
okhttp3.Response test = client.newCall(request).execute();
System.out.println(test.body().string());