多个AsyncHttpClient获取填充一个活动的请求

多个AsyncHttpClient获取填充一个活动的请求

本文介绍了多个AsyncHttpClient获取填充一个活动的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 GameActivity,为了填充布局,我必须多次调用远程API,并想知道使用AsyncHttpClient包。

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/.

我当前设置单个API调用:

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

所以我的getGame Details将是第一个调用,但随后我需要多赚4-6。

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

我的getGameDetails:

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

另一种方法是在AsyncHttpClient的onSuccess方法中调用下一个函数,但这不是

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

问题:我应该在这里使用AsyncHttpClient发出批处理请求吗?

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

感谢任何输入,谢谢。

推荐答案

我无法验证它是否有效,但是我想这里有一个一种接一个地执行请求的方法

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

在您的课程 MyRestClient

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

之后,您只需致电:

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

这篇关于多个AsyncHttpClient获取填充一个活动的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 13:58