How to make concurrent GET calls using Volley?
The problem is that you are initializing Adapter every time that's why your data will be lost once a new API call. I prefer below approach so that can help you, Add Data in ArrayList and notify adapter,
Add this line in onCreate,
staggeredGridAdapter = new StaggeredGridAdapter(StaggeredSearchActivity.this, dataset);
recyclerView.setAdapter(staggeredGridAdapter);
Changes in API Callback Response :
...
dataset.add(new StaggeredCustomCard(user, userpost, postdate));
After loop add below line
staggeredGridAdapter.notifyDataSetChanged();
Changes in Adapter
private ArrayList<StaggeredCustomCard> dataSet;
private Context context;
public MyAdapter(ArrayList<StaggeredCustomCard> dataSet, Context context) {
this.data = data;
this.context = context;
}
Note : Don't create new object on adapter.
The approach just doesn't make a lot of sense. Once you get a response from one of the three endpoints, you seem to create a new Adapter and attach it to the recycler with a random "notifyDataSetChanged" every time...
- Maybe look at using a ViewModel with a service layer and network layer that deal with the business login.
- The ViewModel updates/posts a MutableLiveData> when a callback from one of the Network methods responds from the endpoints...merging the three pieces of data.
- The activity just observes the ViewModel's MutableLiveData and uses a DiffUtil to update the look/cards in the recycler.
The best way is to register your LiveData in the StaggeredSearchActivity in onCreate method and listen for db changes like you did. In the each success responce save its result to db without LiveData. LiveData in onCreate method will be triggered.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ViewModel viewModel = ViewModelProviders.of(this, factory).get(ViewModel.class);
viewModel.getEntity().observe(this, entity -> {
if (entity != null) {
adapter.notifyDataSetChanged(entity );
}
});
requestQueue = Volley.newRequestQueue(this);
Intent intent = getIntent();
String searchText = intent.getStringExtra("searchText");
// Three concurrent API GET Calls
getMediumData(searchText);
getExampleData(searchText);
getGoogleData(searchText);
recyclerView = findViewById(R.id.staggered_recycler_view);
staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
}
ArrayList<StaggeredCustomCard> dataset = new ArrayList<>();
private void getMediumData(String searchText) {
progressBar = findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
String url = UrlConstants.getUrl() + searchText;
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
StaggeredCustomCardDAO.insert();
// TODO just insert to dataBase
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
progressBar.setVisibility(View.INVISIBLE);
}
});
jsonObjectRequest.setRetryPolicy(new
DefaultRetryPolicy(
DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 15,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(jsonObjectRequest);
}
private void getExampleData(String searchText) {
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest
(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
// TODO just insert to dataBase
StaggeredCustomCardDAO.insert();
} catch (
JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(
DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 15,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(jsonArrayRequest);
}
private void getGoogleData(String searchText) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// TODO just insert to dataBase
StaggeredCustomCardDAO.insert();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 15,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(jsonObjectRequest);
}
class ViewModel extends androidx.lifecycle.ViewModel {
private LiveData<StaggeredCustomCard> entity;
public ViewModel(Repository repository) {
entity = repository.getNetworkData();
}
public LiveData<StaggeredCustomCard> getEntity() {
return entity;
}
}
class Repository {
LiveData<StaggeredCustomCard> getNetworkData() {
LiveData<StaggeredCustomCard> localeData = StaggeredCustomCardDAO .getLocaleData();//... todo Read from data base
return localeData;
}
@Dao
public interface StaggeredCustomCardDAO {
@Query("Select * from tbl_staggeredCustomCard ")
LiveData<StaggeredCustomCard> getLocaleData();
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(List<StaggeredCustomCard> items);
}
}