Androidguy Androidguy - 29 days ago 4x
Android Question

How can I wait for the callback?

I want to call my JSON class in a loop, always with another URL, and always pass the result into a RecyclerView. The problem is that I can't place this method into a loop because the loop won't wait for the callback... Any ideas how this could be done?

final JSON json = new JSON("" + page + "&per_page=10", SzabadEuMusorokViewActivity.this);
json.mJSONInterface = new JSONInterface() {
public void startActivityJSON() {
mAdapter.add((ArrayList<Collection>) json.getCollections());
runOnUiThread(new Runnable() {
public void run() {

And I want to increase always the page variable after the call is excecuted. I tried it with a lopp but it won't work.


Always invoke the next call from the callback that you mention. Keep track of where in your array of URLs you are.

something like this (Code untested!)

class JsonFetcher {
   int _start, _end, _current;
   public JsonFetcher(int start, int end) {
   public void fetch_next() {
     _current += 1;
     if (_current < end)
        // Trigger your JSON fetch here and have it call fetch_next in the callback