asynchttpclient OnSuccess方法在活动结束后给出响应

时间:2015-03-27 12:51:59

标签: android asynchttpclient

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

请有人帮助我。

以下是示例代码。

    @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;
}

1 个答案:

答案 0 :(得分:1)

AsyncTask内使用任何网络请求,即使是AsyncHttpClient,也始终是一个好习惯。因此,您应该在onSuccess的{​​{1}}中获取JSON数据,这些数据将放在AsyncHttpClient的{​​{1}}内。然后在doInBackground的{​​{1}}中,您可以设置视图,这些视图也将位于AsyncTask主题中。

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

onPostExecute