java 如何使用 Retrofit 2.0 获取原始响应和请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37088975/
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
How to get raw response and request using Retrofit 2.0
提问by praveen kumar
I am trying to get the raw response using Retrofit2.0.2.
我正在尝试使用 Retrofit2.0.2 获得原始响应。
So far I tried to print the response using following line of code but it prints the address and not the exact response body.
到目前为止,我尝试使用以下代码行打印响应,但它打印的是地址而不是确切的响应正文。
Log.i("RAW MESSAGE",response.body().toString());
Log.i("RAW MESSAGE",response.body().toString());
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
GitApi gitApi = retrofit.create(GitApi.class);
Call<Addresses> call = gitApi.getFeed(user);
call.enqueue(new Callback<Addresses>() {
@Override
public void onResponse(Response<Addresses> response, Retrofit retrofit) {
try {
mDisplayDetails.setText(response.body().getSuburbs().get(0).getText());
**Log.i("RAW MESSAGE",response.body().toString());**
} catch (Exception e) {
mDisplayDetails.setText(e.getMessage());
}
mProgressBar.setVisibility(View.INVISIBLE);
}
@Override
public void onFailure(Throwable t) {
mDisplayDetails.setText(t.getMessage());
mProgressBar.setVisibility(View.INVISIBLE);
}
});
回答by aldok
That's because it's already converted to an object by converter. To get the raw json, you need an interceptor on your Http Client. Thankfully you don't need to write your own class, Square already provide HttpLoggingInterceptor class for you.
那是因为它已经被转换器转换成对象了。要获取原始 json,您需要在 Http 客户端上安装一个拦截器。幸运的是,您不需要编写自己的类,Square 已经为您提供了 HttpLoggingInterceptor 类。
Add this on your app-level gradle
将此添加到您的应用程序级gradle
compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'
And use it in your OkHttpClient
并在您的 OkHttpClient 中使用它
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor).build();
Don't forget to change your HttpClient in Retrofit.
不要忘记在 Retrofit 中更改您的 HttpClient。
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.baseUrl("https://yourapi.com/api/")
.build();
In the Log Cat you'll see the raw json response. Further information is available on Square's OkHttp github.
在 Log Cat 中,您将看到原始 json 响应。Square 的OkHttp github上提供了更多信息。
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 oskarko
You have to use "ResponseBody" from okhttp3 in your call. Then, get "response.body().string()" to get the JSONObject as your server gives to you. It's a good way to catch errors if there are any errors parsing server response to your model object.
您必须在通话中使用 okhttp3 中的“ResponseBody”。然后,获取“response.body().string()”以获取服务器提供给您的 JSONObject。如果解析服务器对模型对象的响应有任何错误,这是一种捕获错误的好方法。
回答by yu zhao
Guess you want to see the raw response body for debugging purpose. There are two ways to do this.
猜猜您想查看原始响应正文以进行调试。有两种方法可以做到这一点。
Using
okhttp3.logging.HttpLoggingInterceptor
as @aldok mentioned.If you want to check some properties of the response object, or convert the json(response body) to POJO manually, you may want to do it like this:
使用
okhttp3.logging.HttpLoggingInterceptor
@aldok 提到的。如果您想检查响应对象的某些属性,或者手动将 json(response body) 转换为 POJO,您可能希望这样做:
Don't use the addConverterFactory while initializing the retrofit client, comment this line.
不要在初始化改造客户端时使用 addConverterFactory,注释这一行。
retrofit = new Retrofit.Builder()
.client(client)
.baseUrl(BASE_URL)
//.addConverterFactory(GsonConverterFactory.create())
.build();
Then consuming the response like this:
然后像这样使用响应:
Call<ResponseBody> topRatedResponseCall = apiService.getTopRatedMoves(API_KEY);
topRatedResponseCall.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
Log.d(LOG_TAG, response.body().string());
int code = response.code();
testText.setText(response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
Hope this would help~
希望这会有所帮助~
回答by Kevin Robatel
Simply use:
只需使用:
Log.i("RAW MESSAGE", response.raw().body().string());
Or:
或者:
Log.i("RAW MESSAGE", response.body().string());