繁体   English   中英

用凌空库加载json数组

[英]loading json array with volley library

我有一个片段,首先从JSON数组中的Web API获取一些数据,然后提取数组中的内容。 然后,我将这两个数组( namesimg_url )提供给自定义网格视图以显示给用户。 但是,我使用volley库获取的JSON数组不起作用。 这是我的代码:

public class selectCategouryFragment extends Fragment {

public selectCategouryFragment() {

}

private ProgressDialog pDialog;
String[] names = { "amir", "imani" };
String[] imgURL = { "asdasd", "asd sad" };
List<Item> items = null;
CustomGrid gridAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_select_categoury,
            container, false);

    pDialog = new ProgressDialog(getActivity());
    // Showing progress dialog before making http request
    pDialog.setMessage("Loading...");
    pDialog.show();

    String url = "http://api.androidhive.info/json/movies.json";
    JsonArrayRequest Jreq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {

                public void onResponse(JSONArray jsonArry) {
                    for (int i = 0; i < jsonArry.length(); i++) {

                        JSONObject obj = null;
                        hidePDialog();
                        try {
                            obj = jsonArry.getJSONObject(i);
                        } catch (JSONException e1) {
                            Log.d("json", "error in taking json obj");
                        }
                        try {
                            names[i] = obj.getString("title");
                            imgURL[i] = obj.getString("image");
                            Log.d("get", obj.getString("title"));
                        } catch (JSONException e) {
                            Log.d("json", "error in taking json obj value ");
                        }

                    }

                    gridAdapter = new CustomGrid(getActivity(), names,
                            imgURL);
                }

            }, new Response.ErrorListener() {

                public void onErrorResponse(VolleyError arg0) {

                }
            });

    AppController.getInstance().addToRequestQueue(Jreq);
    GridView grid = (GridView) rootView
            .findViewById(R.id.selectcat_gridView);

    grid.setAdapter(gridAdapter);

    grid.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            gotItemsFragment(names[position]);

        }
    });

    return rootView;
}

private void hidePDialog() {
    if (pDialog != null) {
        pDialog.dismiss();
        pDialog = null;
    }
}

private void gotItemsFragment(String m) {
    Fragment cardFragment = new CategouryItems(m);
    FragmentManager FM = getFragmentManager();
    FM.beginTransaction().replace(R.id.frame_container, cardFragment)
            .commit();
}
}

长时间显示进度对话框后,它崩溃并在log cat中显示此错误:

    02-01 07:37:17.342: E/AndroidRuntime(1282): FATAL EXCEPTION: main
02-01 07:37:17.342: E/AndroidRuntime(1282): Process: com.plusnet.tashrifat, PID: 1282
02-01 07:37:17.342: E/AndroidRuntime(1282): java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
02-01 07:37:17.342: E/AndroidRuntime(1282):     at plusnet.tashrifat.selectCategouryFragment$1.onResponse(selectCategouryFragment.java:69)
02-01 07:37:17.342: E/AndroidRuntime(1282):     at plusnet.tashrifat.selectCategouryFragment$1.onResponse(selectCategouryFragment.java:1)
02-01 07:37:17.342: E/AndroidRuntime(1282):     at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
02-01 07:37:17.342: E/AndroidRuntime(1282):     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
02-01 07:37:17.342: E/AndroidRuntime(1282):     at android.os.Handler.handleCallback(Handler.java:733)
02-01 07:37:17.342: E/AndroidRuntime(1282):     at android.os.Handler.dispatchMessage(Handler.java:95)
02-01 07:37:17.342: E/AndroidRuntime(1282):     at android.os.Looper.loop(Looper.java:136)
02-01 07:37:17.342: E/AndroidRuntime(1282):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-01 07:37:17.342: E/AndroidRuntime(1282):     at java.lang.reflect.Method.invokeNative(Native Method)
02-01 07:37:17.342: E/AndroidRuntime(1282):     at java.lang.reflect.Method.invoke(Method.java:515)
02-01 07:37:17.342: E/AndroidRuntime(1282):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-01 07:37:17.342: E/AndroidRuntime(1282):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-01 07:37:17.342: E/AndroidRuntime(1282):     at dalvik.system.NativeStart.main(Native Method)

您已经初始化

String[] names = { "amir", "imani" };
String[] imgURL = { "asdasd", "asd sad" };

它看起来jsonArry.length()> 2

所以您需要做的就是将这些更改为:

String[] names = null;
String[] imgURL = null;

并在onResponse()中: for loop之前

names = new String[jsonArry.length()];
imgURL = new String[jsonArry.length()];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM