Android Retrofit - Wait For Response Before Executing Other Code?
Solution 1:
Rather do it like this:
zapposApi.getWhatever(newCallback<List<YourPOJO>>() {
@Overridepublicvoidsuccess(List<YourPOJO> YourPOJO, Response response) {
setupRecyclerView(YourPOJO);
}
@Overridepublicvoidfailure(RetrofitError error) {
//handle your error
}
});
Your api will have an interface that might look like something in the line of :
@GET("/user/current")voidgetCurrentUser(Callback<YourPOJO> response);
This way the actual network call is done in the background and the callback is on the UI thread. You don't have to use async for this!.
Solution 2:
Found the solution to my problem. In my original code I wanted to wait until I recieved an api response before setting up the recyclerView. However, Android doesn't like when you delay setting up the recyclerView for too long so I got this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.onAddFocusables(android.support.v7.widget.RecyclerView, java.util.ArrayList, int, int)' on a null object reference
This brought me back to the dilemna where I didn't have the response data to setup the recyclerview yet. So in setupRecyclerView(), I imported recyclerview from the xml and setup the layout manager but removed the code that created the adapter and added it to the recyclerview. Then I put the adapter creation code in the success() method of my Callback implementation. This fixed the problem!
Found my solution here: Why my android activity is crashing with "No adapter attached; skipping layout" but sporadically?
and the code is below.
public class SearchResultsActivity extends BaseActivity {
// UIprivateToolbar toolbar;
privateSearchView searchbox;
privateRecyclerView rvResults;
// Business LogicprivateString searchQuery;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
searchQuery = getSearchQuery();
setupToolbar();
setupRecyclerView();
searchForProducts(searchQuery);
}
privatevoidsetupToolbar() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
}
privatevoidsetupRecyclerView() {
rvResults = (RecyclerView) findViewById(R.id.rvResults);
rvResults.setHasFixedSize(true);
rvResults.setLayoutManager(newLinearLayoutManager(activityContext));
}
privateStringgetSearchQuery() {
String query = "";
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
query = intent.getStringExtra(SearchManager.QUERY);
}
return query;
}
privatevoidsearchForProducts(String query) {
RestAdapter retrofit = newRestAdapter.Builder()
.setEndpoint(Constants.BASE_URL)
.build();
ZapposAPI api = retrofit.create(ZapposAPI.class);
api.searchZappos(query, newCallback<Response>() {
@Overridepublicvoidsuccess(Response apiResponse, retrofit.client.Response response) {
ResultAdapter adapter = newResultAdapter(apiResponse.getResults());
rvResults.setAdapter(adapter);
}
@Overridepublicvoidfailure(RetrofitError error) {
}
});
}
Post a Comment for "Android Retrofit - Wait For Response Before Executing Other Code?"