活动完成后,asynchttpclient OnSuccess方法给出响应 [英] asynchttpclient OnSuccess method gives response after activity is finished

查看:159
本文介绍了活动完成后,asynchttpclient OnSuccess方法给出响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中遇到了一些困难。我正在根据json响应初始化抽屉,但是当我的活动结束时,我的 OnSuccess方法会给出响应。
如果我在OnSuccess中初始化了arraylist,它也仍然在OnCreate方法中为空,因为OnSuccess方法在活动完成时给出响应。

I am facing some difficulty in my code. I am initializing my drawer from json response, but my 'OnSuccess' method gives response when my activity gets over. also if I initialized arraylist in OnSuccess It still gives empty in OnCreate method because OnSuccess method gives response when activity is finished.

任何人都可以帮助我。

这里是示例代码。

    @Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // Indicate that this fragment would like to influence the set of actions in the action bar.
    setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    mDrawerListView = (ListView) inflater.inflate(
            R.layout.fragment_navigation_drawer, container, false);
    mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    });
    AsyncHttpClient client=new AsyncHttpClient();
    client.addHeader("X-Oc-Merchant-Id", "123");
    client.addHeader("X-Oc-Merchant-Language", "en");
    client.get("http://webshop.opencart-api.com/api/rest/categories", new AsyncHttpResponseHandler() {
        public static final String TAG = "";

        @Override
        public void onSuccess(String response) {
            // Log.i(TAG,"resp= "+response);
            try {
                JSONObject resp = new JSONObject(response);
                if (resp.getString("success").equals("true")) {
                    JSONArray array = resp.getJSONArray("data");
                    cat_count = array.length();
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject ArrObj = array.getJSONObject(i);
                        category.add(ArrObj.getString("name"));
                        category_id.add(ArrObj.getString("category_id"));
                    }

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            mDrawerListView.setAdapter(new ArrayAdapter<String>(
                    getActionBar().getThemedContext(),
                    android.R.layout.simple_list_item_activated_1,
                    android.R.id.text1,

                    new String[]{
                            //getString(R.string.title_section1),
                            //getString(R.string.title_section2),
                            //getString(R.string.title_section3),
                            category.get(0),
                            category.get(1).replaceAll("&amp;","&"),
                            category.get(2),
                            category.get(3),
                            category.get(4),
                            category.get(5).replaceAll("&amp;","&"),
                            category.get(6),
                            category.get(7),
                    }));
            mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
        }
    });


    return mDrawerListView;
}


推荐答案

这始终是一个好习惯使用 AsyncTask 内部的任何网络请求,甚至是 AsyncHttpClient 。因此,您应该在 AsyncHttpClient onSuccess 中获取JSON数据,该数据将放置在<$ c AsyncTask 中的$ c> doInBackground 。然后在 AsyncTask onPostExecute 中,您可以设置视图,该视图也将位于 runOnUi 线程。

It is always a good practice to use any network requests inside AsyncTask, even an AsyncHttpClient. So, you should get your JSON data in the onSuccess of the AsyncHttpClient, which will be placed inside the doInBackground of the AsyncTask. Then in the onPostExecute of the AsyncTask, you can set your views, which will also be inside a runOnUi thread.

以下是简要代码。我已经说明了在哪里获取JSON数据以及在哪里设置视图:

Here are the brief codes. I have stated where to fetch JSON data and where to set the views:

    AsyncTask myTask = new AsyncTask() {

        @Override
        protected Object doInBackground(Object... params) {

            client.get("http://webshop.opencart-api.com/api/rest/categories", new AsyncHttpResponseHandler() {
                public static final String TAG = "";

                @Override
                public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                    // HERE FETCH YOUR JSON DATA

                }

                @Override
                public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {

                }

            });
            return null;
        }

        @Override
        protected void onPostExecute(Object result) {
            super.onPostExecute(result);

            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // HERE SET YOUR VIEWS

                }
            });
        }

    };

    myTask.execute();

这篇关于活动完成后,asynchttpclient OnSuccess方法给出响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆