Multiple AsyncHttpClient get requests to populate one activity

我的梦境 提交于 2019-12-06 05:35:34

问题


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:


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();


来源:https://stackoverflow.com/questions/23485361/multiple-asynchttpclient-get-requests-to-populate-one-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!