0

Как наша деятельность, нам дают этот вид json и попросили отобразить его в пользовательском списке в android. Однако ошибка говорит о том, что индекс 2 выходит за пределы диапазона. Я новичок в программировании на андроиде, пожалуйста, помогите мне.Android-указатель 2 вне диапазона JSON

JSON выглядит следующим образом: -

{ 
    "result": { 
     "status": "1", 
     "message": "SUCCESS", 
     "total_course": 8, 
     "course": [{ 
      "course_id": "3", 
      "parent_course_id": "0", 
      "course_name": "PRINCE2 (Foundation and Practitioner)", 
      "course_price": "24000", 
      "course_price_doller": "140", 
      "course_price_discunt": "22000", 
      "course_price_discunt_doller": "220", 
      "course_discunt_date": "2016-04-04", 
      "course_description": "We are an expert training organization with our faculty having vast experience in consulting and training accredited by Peoplecert on behalf of Axelos.\r\nThis program is for 3 full days.\r\n\r\nXellentro has one of the best Project Management Professionals with large experience of project management training and consulting in this subject across the globe.", 
      "course_image": "http:\/\/arrisofttech.com\/2016\/xellentroapp\/assets\/uploads\/course\/SL571531558C660_COURSE_228x80.png", 
      "total_module": 3, 
      "module": [{ 
       "module_id": "14", 
       "video_price": "12", 
       "video_price_doller": "11", 
       "video_price_discunt": "123", 
       "video_price_discunt_doller": "12", 
       "video_discunt_date": "2016-02-03", 
       "video_type": "1", 
       "video_link": "arrisofttech.com\/2016\/xellentroapp\/video\/sample_2.mp4", 
       "video_image": "http:\/\/arrisofttech.com\/2016\/xellentroapp\/assets\/uploads\/course\/SL571531558C660_COURSE_228x80.png", 
       "video_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." 
      }, { 
       "module_id": "12", 
       "video_price": "12", 
       "video_price_doller": "11", 
       "video_price_discunt": "123", 
       "video_price_discunt_doller": "12", 
       "video_discunt_date": "2016-02-03", 
       "video_type": "0", 
       "video_link": "https:\/\/www.youtube.com\/watch?v=QSaWoca3SjY&list=RDQSaWoca3SjY", 
       "video_image": "http:\/\/arrisofttech.com\/2016\/xellentroapp\/assets\/uploads\/course\/SL571531558C660_COURSE_228x80.png", 
       "video_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." 
      }, { 
       "module_id": "13", 
       "video_price": "12", 
       "video_price_doller": "11", 
       "video_price_discunt": "123", 
       "video_price_discunt_doller": "12", 
       "video_discunt_date": "2016-02-03", 
       "video_type": "0", 
       "video_link": "https:\/\/www.youtube.com\/watch?v=QSaWoca3SjY&list=RDQSaWoca3SjY", 
       "video_image": "http:\/\/arrisofttech.com\/2016\/xellentroapp\/assets\/uploads\/course\/SL571531558C660_COURSE_228x80.png", 
       "video_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." 
      }] 
     }, { 
      "course_id": "1", 
      "parent_course_id": "0", 
      "course_name": "Management of Portfolio (MoP)", 
      "course_price": "51000", 
      "course_price_doller": "510", 
      "course_price_discunt": "40000", 
      "course_price_discunt_doller": "400", 
      "course_discunt_date": "2016-08-09", 
      "course_description": "We are an expert training organization with our faculty having vast experience in consulting and training accredited by Peoplecert on behalf of Axelos.\r\nThis program is for 3 full days.\r\n\r\nXellentro has one of the best Project Portfolio Management Professionals with large experience of project management and consulting in the area across the globe for large organizations and governments.", 
      "course_image": "http:\/\/arrisofttech.com\/2016\/xellentroapp\/assets\/uploads\/course\/SL5715320A694D7_COURSE_128x64.jpg", 
      "total_module": 2, 
      "module": [{ 
       "module_id": "11", 
       "video_price": "12", 
       "video_price_doller": "11", 
       "video_price_discunt": "123", 
       "video_price_discunt_doller": "12", 
       "video_discunt_date": "2016-02-03", 
       "video_type": "0", 
       "video_link": "https:\/\/www.youtube.com\/watch?v=QSaWoca3SjY&list=RDQSaWoca3SjY", 
       "video_image": "http:\/\/arrisofttech.com\/2016\/xellentroapp\/assets\/uploads\/course\/SL5715320A694D7_COURSE_128x64.jpg", 
       "video_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." 
      }, { 
       "module_id": "10", 
       "video_price": "12", 
       "video_price_doller": "11", 
       "video_price_discunt": "123", 
       "video_price_discunt_doller": "12", 
       "video_discunt_date": "2016-02-03", 
       "video_type": "0", 
       "video_link": "https:\/\/www.youtube.com\/watch?v=QSaWoca3SjY&list=RDQSaWoca3SjY", 
       "video_image": "http:\/\/arrisofttech.com\/2016\/xellentroapp\/assets\/uploads\/course\/SL5715320A694D7_COURSE_128x64.jpg", 
       "video_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." 

      }] 
     }] 
    } 
} 

Код: -

Volley.newRequestQueue(getActivity()).add(new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       hidePDialog(); 
       try { 
        JSONObject result = response.getJSONObject("result"); 
        JSONArray jsonArray = result.getJSONArray("course"); 

        System.out.println("Course: == >" + result.getJSONArray("course")); 
        for (int i = 0; i < jsonArray.length(); i++) { 
         JSONObject trainingObj = jsonArray.getJSONObject(i); 
         Movie movie = new Movie(); 
         movie.setCourse_id(trainingObj.getString("course_id")); 
         movie.setParent_course_id(trainingObj.getString("parent_course_id")); 
         movie.setCourse_name(trainingObj.getString("course_name")); 
         movie.setCourse_price(trainingObj.getString("course_price")); 
         movie.setCourse_price_dollar(trainingObj.getString("course_price_doller")); 
         movie.setCourse_price_discount_dollar(trainingObj.getString("course_price_discunt_doller")); 
         movie.setCourse_discount_date(trainingObj.getString("course_discunt_date")); 
         movie.setCourse_description(trainingObj.getString("course_description")); 
         movie.setCourse_image(trainingObj.getString("course_image")); 

         JSONArray module = trainingObj.getJSONArray("module"); 

         for (int j = 0; j < module.length(); j++) { 
          JSONObject moduleObj = module.getJSONObject(i); 
          movie.setModule_id(moduleObj.getString("module_id")); 
          movie.setVideo_price(moduleObj.getString("video_price")); 
          movie.setVideo_price_doller(moduleObj.getString("video_price_doller")); 
          movie.setVideo_price_discunt(moduleObj.getString("video_price_discunt")); 
          movie.setVideo_price_discunt_doller(moduleObj.getString("video_price_discunt_doller")); 
          movie.setVideo_discunt_date(moduleObj.getString("video_discunt_date")); 
          movie.setVideo_type(moduleObj.getString("video_type")); 
          movie.setVideo_link(moduleObj.getString("video_link")); 
          movie.setVideo_image(moduleObj.getString("video_image")); 
          movie.setVideo_description(moduleObj.getString("video_description")); 

         } 

         trainingList.add(movie); 
// 

         System.out.println("Setting up in ListView"); 
//      System.out.println("Result:- " + newsId + " " + dateTime + " " + newsType + " " + title + " " + description + " " + mainImageURL); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
       adapter.notifyDataSetChanged(); 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       new AlertDialog.Builder(getActivity()) 
         .setTitle("No Connectivity ") 
         .setMessage("Please check your internet connectivity!") 
         .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           // continue with delete 
          } 
         }) 
           //.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { 
           //public void onClick(DialogInterface dialog, int which) { 
           // do nothing 
           //} 
           //}) 
         .setIcon(android.R.drawable.ic_dialog_alert) 
         .show(); 
       hidePDialog(); 
      } 
     })); 

Log: -

05-03 03:36:17.019 25234-25234/com.sentechlabs.xellentroapp W/System.err: org.json.JSONException: Index 2 out of range [0..2) 
05-03 03:36:17.020 25234-25234/com.sentechlabs.xellentroapp W/System.err:  at org.json.JSONArray.get(JSONArray.java:293) 
05-03 03:36:17.020 25234-25234/com.sentechlabs.xellentroapp W/System.err:  at org.json.JSONArray.getJSONObject(JSONArray.java:521) 
05-03 03:36:17.020 25234-25234/com.sentechlabs.xellentroapp W/System.err:  at com.sentechlabs.xellentroapp.TrainingFragment$1.onResponse(TrainingFragment.java:99) 
05-03 03:36:17.020 25234-25234/com.sentechlabs.xellentroapp W/System.err:  at com.sentechlabs.xellentroapp.TrainingFragment$1.onResponse(TrainingFragment.java:72) 
05-03 03:36:17.020 25234-25234/com.sentechlabs.xellentroapp W/System.err:  at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65) 
05-03 03:36:17.020 25234-25234/com.sentechlabs.xellentroapp W/System.err:  at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 
05-03 03:36:17.020 25234-25234/com.sentechlabs.xellentroapp W/System.err:  at android.os.Handler.handleCallback(Handler.java:815) 
05-03 03:36:17.020 25234-25234/com.sentechlabs.xellentroapp W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:104) 
05-03 03:36:17.020 25234-25234/com.sentechlabs.xellentroapp W/System.err:  at android.os.Looper.loop(Looper.java:194) 
05-03 03:36:17.020 25234-25234/com.sentechlabs.xellentroapp W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:5651) 
05-03 03:36:17.020 25234-25234/com.sentechlabs.xellentroapp W/System.err:  at java.lang.reflect.Method.invoke(Native Method) 
05-03 03:36:17.020 25234-25234/com.sentechlabs.xellentroapp W/System.err:  at java.lang.reflect.Method.invoke(Method.java:372) 
05-03 03:36:17.020 25234-25234/com.sentechlabs.xellentroapp W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) 
05-03 03:36:17.020 25234-25234/com.sentechlabs.xellentroapp W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 
+0

Возможно, вам стоит подумать об использовании Gson. Весь этот код будет намного меньше. –

ответ

2

Вы используете неправильную переменную счетчика во внутреннем цикле. Должно быть getJSONObject(j):

for (int j = 0; j < module.length(); j++) { 
    JSONObject moduleObj = module.getJSONObject(i); 
Смежные вопросы