コード例 #1
0
  private void makeJsonArrayRequest() {
    pDialog.show();

    JsonArrayRequest req =
        new JsonArrayRequest(
            trainingsUrl,
            new Response.Listener<JSONArray>() {
              @Override
              public void onResponse(JSONArray response) {

                try {
                  for (int i = 0; i < response.length(); i++) {
                    JSONObject objResponse = (JSONObject) response.get(i);

                    // Récupérer trainings
                    Trainings training = new Trainings();
                    training.setName(objResponse.getString("title"));
                    training.setCode(objResponse.getString("ean13"));

                    list.add(training);
                  }

                  TrainAdapter = new TrainingsListAdapter(getActivity(), list);
                  TrainList.setAdapter(TrainAdapter);
                  pDialog.dismiss();

                } catch (JSONException e) {
                  e.printStackTrace();
                  /*Toast.makeText(getApplicationContext(),
                  "Error: " + e.getMessage(),
                  Toast.LENGTH_LONG).show();*/
                }
                pDialog.dismiss();
              }
            },
            new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                /*Toast.makeText(getApplicationContext(),
                error.getMessage(), Toast.LENGTH_LONG).show();*/
                pDialog.dismiss();
              }
            });
    AppController.getInstance().addToRequestQueue(req, "jreq");
  }
コード例 #2
0
  private void makeJsonObjectRequest() {
    pDialog.show();

    JsonObjectRequest req =
        new JsonObjectRequest(
            trainingUrl,
            new Response.Listener<JSONObject>() {
              @Override
              public void onResponse(JSONObject response) {

                try {
                  // TRAINING TITLE
                  training.setName(response.getString("title"));
                  training_title.setText(response.getString("title"));

                  // TRAINING VIDEO
                  JSONObject teaserInfo = response.getJSONObject("teaser_info");
                  videoUrl =
                      String.format(
                          "http://videos.elephorm.com/%1$s/video",
                          teaserInfo.getString("video_url"));
                  training_video.setVideoURI(Uri.parse(videoUrl));

                  // SOMMAIRE
                  JSONArray items = response.getJSONArray("items");

                  // Récupération de l'objet dans une ArrayList pour pouvoir la réutiliser par la
                  // suite
                  for (int i = 0; i < items.length(); i++) {
                    JSONObject childItems = items.getJSONObject(i);
                    Course course = new Course();
                    course.setName(childItems.getString("title"));
                    course.set_id(childItems.getString("_id"));
                    courses.add(course);
                  }

                  // i = 1 pour éviter d'afficher le premier objet qui correspond à la liste des
                  // chapitres
                  for (int i = 1; i < items.length(); i++) {
                    JSONObject childItems = items.getJSONObject(i);
                    JSONArray children = childItems.getJSONArray("children");
                    if (childItems.getString("type").equals("chapter")) {
                      Group gru = new Group();
                      gru.setName(childItems.getString("title"));
                      list.add(gru);
                      ch_list = new ArrayList<Child>();
                      for (int j = 0; j < children.length(); j++) {
                        Child ch = new Child();
                        for (int k = 0; k < courses.size(); k++) {
                          if (children.get(j).equals(courses.get(k).get_id())) {
                            ch.setName(courses.get(k).getName());
                            ch.setId(courses.get(k).get_id());
                          }
                        }
                        ch_list.add(ch);
                      }
                      gru.setItems(ch_list);
                    }
                  }

                  ExpAdapter = new ExpandListAdapter(getActivity(), list);
                  ExpandList.setAdapter(ExpAdapter);
                  pDialog.dismiss();

                } catch (JSONException e) {
                  e.printStackTrace();
                  pDialog.dismiss();
                }

                pDialog.dismiss();
              }
            },
            new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                /*Toast.makeText(getApplicationContext(),
                error.getMessage(), Toast.LENGTH_LONG).show();*/
                pDialog.dismiss();
              }
            });
    AppController.getInstance().addToRequestQueue(req, "jreq");
  }