Androidguy Androidguy - 1 year ago 59
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.

Answer Source

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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download