用齐射库加载json数组

时间:2022-10-22 20:37:16

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 :

我有一个片段,首先我从JSON数组中的Web API获取一些数据并提取数组中的什么。然后我将这两个数组(名称,img_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();
}
}

after a long time that show progress dialog , its crash and show this error s in log cat :

经过很长一段时间显示进度对话框,它崩溃并在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)

1 个解决方案

#1


1  

you have initialized

你已经初始化了

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

it looks jsonArry.length() > 2

它看起来像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

在onResponse()中:在for循环之前

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

#1


1  

you have initialized

你已经初始化了

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

it looks jsonArry.length() > 2

它看起来像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

在onResponse()中:在for循环之前

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