recently I started using retrofit and I'm having a hard time trying to understand how it works.
I've came across with this, while trying to pass a variable, which is initialized in onResponse(), to another method to use it later.
The below code is the way I could think of to try to address the solution, but then i realized that the method loadList returned a null ItemList.
Question 1) I would like to know why is this happening, am i missing some knowledge about java or this approach is not the best solution?
Also, I noticed that if I want to use the value of list inside onResponse I am able to do so, with the value of response.body()
public ItemList loadList()
Call<ItemList> call = retrofitInit().loadList();
call.enqueue(new Callback<ItemList>() {
@Override
public void onResponse(Call<ItemList> call, Response<ItemList> response) {
list = response.body();
System.out.println(item.getItem().get(0).getName());
}
@Override
public void onFailure(Call<ItemList> call, Throwable t) {
System.out.println("FAILURE" + t.toString());
}
});
return list;
}
** UPDATE: **
private RestClient retrofitInit() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.0.49/Android/")
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit.create(RestClient.class);
}
Question 2) Does anyone has any better solutions to do this? I've seen a lot of people using RxJava but I don't really want to get into it yet.
I appreciate all your comments, Thanks!