[英]How to check if json from network is converted to my object or not using retrofit?
I am getting network response correctly (I checked it using HttpLoggingInterceptor.Level.BODY
), but my viewModel LiveData is still empty.我得到了正确的网络响应(我使用HttpLoggingInterceptor.Level.BODY
检查了它),但我的 viewModel LiveData 仍然是空的。
projectMembers = Transformations.switchMap(selectedProjectId, input -> cuttingListRepository.getProjectMembersByProjectUnit(input));
In above code projectMembers.getValue()
is still null在上面的代码中projectMembers.getValue()
仍然为 null
I see no error in console, It could be that my json response is not getting converted to object how to be sure about this?我在控制台中没有看到错误,可能是我的 json 响应没有转换为对象如何确定这一点?
I m observing it inside fragment's onCreateView --我在片段的 onCreateView 中观察它——
cuttingComponentViewModel.projectMembers.observe(this.getViewLifecycleOwner(), new Observer<List<PojoProjectMember>>() {
@Override
public void onChanged(List<PojoProjectMember> pojoProjectMembers) {
System.out.println("getting observed");
}
});
LiveData will always be null, unless it is observed by something. LiveData 将始终为空,除非它被某些东西观察到。 Ensure you are observing the data from your activity / fragment.确保您正在观察来自您的活动/片段的数据。
change your ApiClient class for retrofit with this code in which gson convertor factory is used使用此代码更改您的 ApiClient 类以进行改造,其中使用了 gson 转换器工厂
public static ApiInterface retrofit = null;
public static ApiInterface getClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS);
if (retrofit == null) {
Retrofit retro = new Retrofit.Builder()
.baseUrl(Constant.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
retrofit = retro.create(ApiInterface.class);
}
return retrofit;
}
and serialize your pojo class like that并像这样序列化你的 pojo 类
public class ResponseModel {
@SerializedName("success")
public boolean success;
@SerializedName("message")
public String message;
@SerializedName("status")
public String status;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
In serialize name used api response keywords在序列化名称中使用 api 响应关键字
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.