0

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.selectCategouryFragment1ドル.onResponse(selectCategouryFragment.java:69)
02-01 07:37:17.342: E/AndroidRuntime(1282): at plusnet.tashrifat.selectCategouryFragment1ドル.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)
asked Feb 1, 2015 at 12:22
3
  • How is it not working? Commented Feb 1, 2015 at 12:31
  • its dont load any json data . Commented Feb 1, 2015 at 12:32
  • it just show the progress dialog and dont exit from that Commented Feb 1, 2015 at 12:33

1 Answer 1

1

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()];
answered Feb 1, 2015 at 12:41
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.