java - 改造 call.enqueue 代码 200,但 response.body() 为空
问题描述
JSON
我从response.body()
我的API获取时遇到问题。
Logcat 显示请求成功,但是当我想将正文复制到我的模型对象时,它为空。
我已经将唯一的键和值缩短JSON
为一个键和值(logcat 的结尾),但即便如此我也无法读取响应。
日志猫:
D/OkHttp: <-- 200 http://10.0.2.2:8080/groups?page=1&limit=25 (33ms)
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 14 Jan 2019 19:31:10 GMT
D/OkHttp: [{"name":"Delfiny"}]
<-- END HTTP (20-byte body)
型号类:
public class GroupsResponseModel {
@SerializedName("name")
@Expose
private String name;
public GroupsResponseModel() {
}
public GroupsResponseModel(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
接口接口:
@GET("groups")
Call<GroupsResponseModel> getGroups(@Query("page") int page,
@Query("limit") int limit);
改造和拦截器:
public static Retrofit getClient()
{
HttpLoggingInterceptor interceptor = new
HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient().newBuilder().
addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws
IOException {
Request originalRequest = chain.request();
Request.Builder builder = originalRequest.newBuilder().header(Utils.getAuthKey(),
Utils.getAuthValue());
Request newRequest = builder.build();
return chain.proceed(newRequest);
}
})
.addInterceptor(interceptor)
.build();
retrofit = new Retrofit.Builder()
.baseUrl(Utils.getURL())
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
调用队列:
Call<GroupsResponseModel> call = apiInterface.getGroups(1, 25);
call.enqueue(new Callback<GroupsResponseModel>() {
@Override
public void onResponse(Call<GroupsResponseModel> call, Response<GroupsResponseModel> response) {
groupsResponseModel = response.body();
Toast.makeText(GroupActivity.this, "response: " + groupsResponseModel.getName(), Toast.LENGTH_LONG).show();
//groups = new ArrayList<>(Arrays.asList(groupsResponseModel.getName()));
groupAdapter = new GroupAdapter(groups);
recyclerView.setAdapter(groupAdapter);
}
@Override
public void onFailure(Call<GroupsResponseModel> call, Throwable t) {
}
});
需要解析预期的结果。
解决方案
如果您绝对确定服务器发送了正确的 json - 请检查您的 pojo 类。从删除@Exposed注解开始
推荐阅读
- php - How to insert href link in this button tag
- python - Python Regex: AttributeError: 'str' object has no attribute 'Match'
- javascript - 创建递归表单元素
- azure - Kubernetes Service does not map the right port
- php - 为什么 php artisan: schedule run 命令不执行 artisan 命令?
- laravel - 在 Laravel 中为事件定义队列
- javascript - 在模型末尾附加两位小数
- php - ArgumentCountError Codeigniter
- python - 如何以pythonic方式切片列表?
- airflow - 如何设置适当的权限以在 Cloud Composer 中运行 KubernetesPodOperator?