0

我的代码遇到了一些困难。我正在从 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;
}
4

1 回答 1

1

在内部使用任何网络请求始终是一个好习惯AsyncTask,甚至是AsyncHttpClient. 因此,您应该将 JSON数据放在onSuccess. 然后在中,您可以设置您的视图,这也将在一个线程内。AsyncHttpClientdoInBackgroundAsyncTaskonPostExecuteAsyncTaskrunOnUi

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

    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();
于 2015-03-27T13:26:15.797 回答