[英]Retrofit call.enqueue code 200, but response.body() is empty
I have a problem with getting JSON
from response.body()
from my API . 我从API的
response.body()
获取JSON
时遇到问题。
Logcat is showing that request is successful, but when I want to copy the body to my model object, then it is null. Logcat显示请求成功,但是当我要将主体复制到模型对象时,它为null。
I already shortened the JSON
only to one key and value (end of logcat), but even then I cant read the response. 我已经只将
JSON
缩短为一个键和一个值(logcat的结尾),但是即使这样我也无法读取响应。
Logcat: 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)
Model class: 型号类别:
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;
}
}
ApiInterface: Api接口:
@GET("groups")
Call<GroupsResponseModel> getGroups(@Query("page") int page,
@Query("limit") int limit);
Retrofit and Interceptor: 改造和拦截器:
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 enqueue: 呼叫入队:
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) {
}
});
Expected result needs to be parsed. 预期结果需要解析。
If you're absolutely sure that server sends proper json - check your pojo class. 如果您完全确定服务器会发送正确的json,请检查您的pojo类。 Start with deleting @Exposed annotation
首先删除@Exposed注解
我建议您使用Profiler之类的Android Studio调试工具监视可在Profiler的“网络”部分轻松访问的网络(API调用)请求和响应主体,请查看以下链接: Android Profiler
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.