改造:来自 gson 转换体的原始响应体
Retrofit: Raw response body from a gson converted body
我正在使用以下库进行改造。
'com.squareup.retrofit2:retrofit:2.5.0'
'com.squareup.okhttp:okhttp:2.7.5'
'com.squareup.retrofit2:converter-gson:2.5.0'
'com.squareup.okhttp3:logging-interceptor:3.10.0'
如何在 onResponse 回调中获取原始响应?我已经搜索过它并得到了很多现在没有帮助的解决方案。我试过 response.raw().body.string()
和 response.body().source().toString()
抛出 cannot read body from a converted body。我也试过 response.body().string()
但在这种情况下 .string()
未解决。我可以使用拦截器记录响应,但我需要在我的 onResponse()
回调中得到该响应,而不仅仅是在 logcat 中打印。
我的改装客户:
public static ApiService getClient(Context context) {
if (retrofit == null) {
if (BuildConfig.FLAVOR.equalsIgnoreCase("dev")){
retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.client(okHttpClient)
.build();
}else {
retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.client(SelfSigningClientBuilder.createClient(context))
.build();
}
}
return retrofit.create(ApiService.class);
}
我的 Retyrofit 界面:
@retrofit2.http.POST("weekly_driver_earning_report/")
@retrofit2.http.FormUrlEncoded
Call<List<DailyEarnings>> getDriverWeeklyEarnings(@retrofit2.http.Field("access_token") String access_token, @retrofit2.http.Field("start_date") String start_date, @retrofit2.http.Field("end_date") String end_date);
跟随,我后来解决了我的问题。我将调用类型定义为特定模型 class,这就是无法访问 response.body().string() 的原因,将我的调用类型从列表更改为 ResponseBody,我可以访问 response.body()。字符串().
我正在使用以下库进行改造。
'com.squareup.retrofit2:retrofit:2.5.0'
'com.squareup.okhttp:okhttp:2.7.5'
'com.squareup.retrofit2:converter-gson:2.5.0'
'com.squareup.okhttp3:logging-interceptor:3.10.0'
如何在 onResponse 回调中获取原始响应?我已经搜索过它并得到了很多现在没有帮助的解决方案。我试过 response.raw().body.string()
和 response.body().source().toString()
抛出 cannot read body from a converted body。我也试过 response.body().string()
但在这种情况下 .string()
未解决。我可以使用拦截器记录响应,但我需要在我的 onResponse()
回调中得到该响应,而不仅仅是在 logcat 中打印。
我的改装客户:
public static ApiService getClient(Context context) {
if (retrofit == null) {
if (BuildConfig.FLAVOR.equalsIgnoreCase("dev")){
retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.client(okHttpClient)
.build();
}else {
retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.client(SelfSigningClientBuilder.createClient(context))
.build();
}
}
return retrofit.create(ApiService.class);
}
我的 Retyrofit 界面:
@retrofit2.http.POST("weekly_driver_earning_report/")
@retrofit2.http.FormUrlEncoded
Call<List<DailyEarnings>> getDriverWeeklyEarnings(@retrofit2.http.Field("access_token") String access_token, @retrofit2.http.Field("start_date") String start_date, @retrofit2.http.Field("end_date") String end_date);
跟随