简体   繁体   中英

loading json array with volley library

I have an fragment that in first I get some data from web API in JSON array and extract whats in the array. Then I give this two array ( names , img_url ) to custom grid view to show user. But, my JSON array that I use volley library to get that is not working. Here is my code :

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

after a long time that show progress dialog , its crash and show this error s in 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)

you have initialized

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

it looks jsonArry.length() > 2

so what you need to do is change these to:

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

and in onResponse(): before the for loop

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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