[我正在尝试使用Retrofit
和Gson
加载由Web服务生成的JSON格式的一些非常简单的数据,并尝试实现MVVM
架构,但是当获得null
值时从ViewModel
调用数据:我已经看过很多SO问题(或可能不是很多),但无法弄清楚我在哪里以及为什么遇到此问题。
所以,我有这个
JSON响应:这可能是这个:
{
"ok": [
{
"id": 1,
"username": "test1",
"pass": "123",
"reg_date": "2020-05-20 00:00:00"
}
]
}
或此:
{
"error": [
{
"message":"user doesn't exists"
}
]
}
我已经为每种响应类型创建了对应的POJO,为简单起见,我将其省略。但是我也创建了一个通用的POJO来处理响应(感谢@CôngHải,)
** POJOGenericResponse.java *
public class POJOGenericResponse{
@SerializedName("ok")
private LiveData<ArrayList<POJOUsr>> user;
@SerializedName("error")
private LiveData<ArrayList<POJOError>> error;
//Constructor and Getters...
}
JsonApi.java
public interface JsonApi {
@GET("SelectAllUsr.php")
Call<LiveData<POJOGenericResponse>> getGenericResponse();
}
我不知道如何根据其目的和名称约定来调用该类
RetrofitInstance.java
public class RetrofitInstance {
private static String URL_BASE = "http://192.168.0.35/app_android/webservices/";
private static Retrofit retrofit;
private static Gson gson;
public Retrofit getRetrofitInstance(){
gson = new GsonBuilder().setLenient().create();
if(retrofit == null){
retrofit = new Retrofit.Builder()
.baseUrl(URL_BASE)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}
LoginRepository.java
public class LoginRepository{
private RetrofitInstance retrofitInstance;
private JsonApi jsonApi;
private LiveData<POJOGenericResponse> genericResponse;
private Call<LiveData<POJOGenericResponse>> call;
public LoginRepository() {
retrofitInstance = new RetrofitInstance();
jsonApi = retrofitInstance.getRetrofitInstance().create(JsonApi.class);
call = jsonApi.getRespuestaGenerica();
}
//TODO: This should be done in another class??
public LiveData<POJOGenericResponse> getGenericResponse() {
call.clone().enqueue(new Callback<LiveData<POJOGenericResponse>>() {
@Override
public void onResponse(Call<LiveData<POJOGenericResponse>> call, Response<LiveData<POJOGenericResponse>> response) {
if (response.isSuccessful()) {
genericResponse= response.body();
} else {
//
}
}
@Override
public void onFailure(Call<LiveData<POJOGenericResponse>> call, Throwable t) {
//
}
});
return genericResponse;
}
}
LoginViewModel.java
public class LoginViewModel extends AndroidViewModel {
private RepositorioLogin loginRepository;
private MutableLiveData<POJOGenericResponse> serverResponse;
public ViewModelLogin(@NonNull Application application) {
super(application);
loginRepository = new RepositorioLogin();
serverResponse = new MutableLiveData<>();
}
public void setServerResponse(){
serverResponse.setValue(loginRepository.getGenericResponse().getValue());
}
public LiveData<POJORespuestaGenerica> getServerResponse(){
return serverResponse;
}
}
此处应显示数据
FragmentData.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_data, container, false);
TextView exampleText = v.findViewById(R.id.tv_test);
LoginViewModel loginViewModel = new ViewModelProvider(requireActivity()).get(LoginViewModel.class);
loginViewModel.getServerResponse().observe(requireActivity(), new Observer<POJORespuestaGenerica>() {
@Override
public void onChanged(POJOGenericResponse pojoGenericResponse) {
if (pojoGenericResponse.getUser() != null) {
exampleText.setText(pojoGenericResponse.getUser().getValue().get(0).getUsername());
}
if (pojoGenericResponse.getError() != null) {
exampleText.setText(pojoGenericResponse.getError().getValue().get(0).getMensaje());
}
}
});
调试时,我得到的错误是
response =找不到本地变量'response'serverResponse.setValue(loginRepository.getGenericResponse()。getValue()); \ n = java.lang.NullPointerExceptionresponse =找不到局部变量'response'
单独描述问题,但无法解决。
此外,对于MVVM
架构实施的任何建议也将不胜感激。
之前,非常感谢
看起来像是不正确的位-