4

I have a "GameActivity" and in order to populate the layout I have to make multiple calls to a remote API and wondering the best way to accomplish this using the AsyncHttpClient package http://loopj.com/android-async-http/.

My current set-up for a single API call:

public class MainActivity extends Activity implements AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener{

    ListView mainListView;
    JSONMainAdapter mJSONAdapter;
    SwipeRefreshLayout swipeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        swipeLayout = (SwipeRefreshLayout) findViewById(R.id.main_swipe_container);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

        mainListView = (ListView) findViewById(R.id.main_listview);
        mainListView.setOnItemClickListener(this);

        mJSONAdapter = new JSONMainAdapter(this, getLayoutInflater());
        mainListView.setAdapter(mJSONAdapter);

        getGameDetails();
    }

So my getGame Details will be the first call, but then I'll need to make 4-6 more.

My getGameDetails:

private void getGames() {

        swipeLayout.setRefreshing(true);

        MyRestClient.get("games", null, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                swipeLayout.setRefreshing(false);
                Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_LONG).show();
                mJSONAdapter.updateData(jsonObject.optJSONArray("games"));
            }

            @Override
            public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
                swipeLayout.setRefreshing(false);
                Toast.makeText(getApplicationContext(), "Error: " + statusCode + " " + throwable.getMessage(), Toast.LENGTH_LONG).show();
                Log.e("ERROR", statusCode + " " + throwable.getMessage());
            }
        });
    }

So my thoughts are to add a function for each call I need and just call them one after the other in my onCreate like so:

getGameDetails();
getGameCallA();
getGameCallB();
getGameCallC();

The other method would be to call the next function in the onSuccess method of AsyncHttpClient but that doesn't seem right.

Question: is there a "batch request" with AsyncHttpClient that I should be using here?

Any input appreciated, thanks.

1 Answers1

4

I have no way to verify will it work or not, but I guess there is a way to execute your requests one after another

In your class MyRestClient:

private static AsyncHttpClient client = new AsyncHttpClient();
static {
    client.setThreadPool(Executors.newSingleThreadExecutor());
}

after that you just call:

getGameDetails();
getGameCallA();
getGameCallB();
getGameCallC();
Igor Tyulkanov
  • 5,487
  • 2
  • 32
  • 49
  • I want to note the way to call the next function in the onSuccess method looks more correctly. Because in my case you will get sequentially executing http requests, but you will not get warranty that a next http request will executed slower then code in previous onSucess block. Besides what if your first request returns onError? Whether you want to continue executing next requests? – Igor Tyulkanov May 06 '14 at 03:52
  • The more I think about it I think you're correct - I'll just use the onSuccess to call the other api calls. At first inspection it just seemed like a very linear non-OO approach. So assuming there is no "batch" request feature hidden in the AsyncHttpClient than this will suffice. – canigetabreak May 07 '14 at 15:21