private void getRegisteredEventRequest() {

    final JsonObjectRequest jsonObjectRequest =
        new JsonObjectRequest(
            getRegisteredEventsURL(),
            new Response.Listener<JSONObject>() {
              @Override
              public void onResponse(JSONObject response) {
                Log.d("profile response", response.toString());
                try {
                  JSONObject data = response.getJSONObject("data");
                  String status = response.getString("status");

                  if (status.equals("User Profile Info")) {
                    JSONArray arr = new JSONArray();

                    if (data.has("events_register")) {
                      arr = data.getJSONArray("events_register");

                      register_button.setText("Register");

                      for (int i = 0; i < arr.length(); i++) {
                        Log.v("%&#$", arr.getString(i));

                        if (event.getName().equals(arr.getString(i))) {
                          current_event_registered = true;
                          register_button.setText("Unregister");
                          register_button.setVisibility(View.VISIBLE);
                        }
                      }
                    }
                  }
                } catch (JSONException e) {
                  e.printStackTrace();
                }
              }
            },
            new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
              }
            }) {
          @Override
          public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> map = new HashMap<>();
            map.put(
                "Authorization",
                "bearer " + new PersonalData(EventRegisterActivity.this).getToken());
            return map;
          }
        };

    jsonObjectRequest.setRetryPolicy(
        new DefaultRetryPolicy(10000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    MySingleton.getInstance(MyApplication.getAppContext()).addToRequestQueue(jsonObjectRequest);
  }
  private void getRegistrationRequest(final EventClass current_event) {

    final JsonObjectRequest jsonObjectRequest =
        new JsonObjectRequest(
            Request.Method.PUT,
            getURLForRegistration(current_event.getName()),
            new Response.Listener<JSONObject>() {
              @Override
              public void onResponse(JSONObject response) {
                Log.d("RESPONSE-Registration", response.toString());

                try {
                  String status = response.getString("status");
                  loadToast.success();

                  Toast.makeText(EventRegisterActivity.this, status, Toast.LENGTH_LONG).show();

                  if (response.has("validity")) {
                    int v = response.getInt("validity");
                    if (v == 1) {
                      register_button.setText("Unregister");
                      current_event_registered = true;
                    } else {
                      register_button.setText("Register");
                      current_event_registered = false;
                    }
                  }

                  register_button.setVisibility(View.VISIBLE);

                } catch (JSONException e) {
                  e.printStackTrace();
                  loadToast.error();
                }
              }
            },
            new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                loadToast.error();
                error.printStackTrace();
              }
            }) {
          @Override
          public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> map = new HashMap<>();
            map.put(
                "Authorization",
                "bearer " + new PersonalData(EventRegisterActivity.this).getToken());
            return map;
          }
        };

    jsonObjectRequest.setRetryPolicy(
        new DefaultRetryPolicy(10000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    MySingleton.getInstance(MyApplication.getAppContext()).addToRequestQueue(jsonObjectRequest);
  }
  private void getRequest() {

    getRegisteredEventRequest();
    Log.v("Sending request", "for team " + event.getTeamname() + "/" + event.getName());

    final JsonObjectRequest jsonObjectRequest =
        new JsonObjectRequest(
            getURL(event.getTeamname(), event.getName()),
            new Response.Listener<JSONObject>() {
              @Override
              public void onResponse(JSONObject response) {
                Log.d("RESPONSE", response.toString());

                loadToast.success();

                try {
                  String status = response.getString("status");
                  if (status.equals("Success!")) {
                    JSONArray data = response.getJSONArray("data");

                    if (data.length() == 0) {
                      text.setText("Details about event is currently unavailable.\n\n");

                      error_image.setVisibility(View.VISIBLE);
                    } else {
                      for (int j = 0; j < data.length(); j++) {
                        JSONObject dataValue = (JSONObject) data.get(j);

                        if (dataValue.has("_id")) {
                          event.setId(dataValue.getString("_id"));
                          Log.v("eventcard-respose", "id found");
                        }

                        if (dataValue.has("Ename")) {
                          event.setName(dataValue.getString("Ename"));
                          Log.v("eventcard-respose", "ename found");
                        }

                        if (dataValue.has("Dname")) {
                          event.setDname(dataValue.getString("Dname"));
                          Log.v("eventcard-respose", "dname found");
                        } else {
                          event.setDname("NA");
                        }

                        if (dataValue.has("Tname")) {
                          event.setTeamname(dataValue.getString("Tname"));
                          Log.v("eventcard-respose", "tname found");
                        } else {
                          event.setTeamname("NA");
                        }

                        if (dataValue.has("shortD")) {
                          event.setShort_des(dataValue.getString("shortD"));
                          Log.v("eventcard-respose", "shortD found");
                        } else {
                          event.setShort_des("Currently Not Available");
                        }

                        if (dataValue.has("longD")) {
                          event.setLong_des(dataValue.getString("longD"));
                          Log.v("eventcard-respose", "longD found");
                        } else {
                          event.setLong_des(" ");
                        }

                        if (dataValue.has("Contact")) {
                          event.setContact(dataValue.getString("Contact"));
                          // Log.v("eventcard-respose", "longD found");
                        } else {
                          event.setContact("NA");
                        }

                        if (dataValue.has("timeline")) {
                          event.setTimeline(dataValue.getString("timeline"));
                          // Log.v("eventcard-respose", "longD found");
                        } else {
                          event.setTimeline("NA");
                        }

                        if (dataValue.has("__v")) {
                          event.set__v(dataValue.getString("__v"));
                          // Log.v("eventcard-respose", "longD found");
                        }

                        if (dataValue.has("rules")) {
                          JSONArray rulesResponse = dataValue.getJSONArray("rules");

                          ArrayList<String> ruleslist = new ArrayList<>();

                          for (int k = 0; k < rulesResponse.length(); k++) {
                            ruleslist.add(rulesResponse.get(k).toString());
                          }

                          if (rulesResponse.length() == 0) {
                            ruleslist.add("NA");
                          }

                          event.setRules(ruleslist);
                        }
                      }

                      String toshow = "";
                      if (!event.getTeamname().equals("NA")) {
                        toshow += "by " + event.getTeamname();
                      }

                      if (!event.getDname().equals("NA")) {
                        toshow += " ( " + event.getDname() + " )";
                      }
                      toshow += "\n\n";

                      if (!event.getTimeline().equals("NA")) {
                        toshow += event.getTimeline() + "\n\n";
                      }

                      if (!event.getShort_des().equals("NA")) {
                        toshow += event.getShort_des() + "\n\n";
                      }

                      if (event.getRules() != null) {

                        toshow += "Rules:-\n\n";

                        if (!event.getRules().get(0).equals("NA")) {
                          for (int i = 0; i < event.getRules().size(); i++) {

                            toshow += "" + (i + 1) + ". " + event.getRules().get(i) + "\n\n";
                          }
                        } else {
                          toshow += "NA\n\n";
                        }
                      }

                      if (!event.getContact().equals("NA")) {
                        toshow += "Contact Number: " + event.getContact();
                      }

                      text.setText(toshow);
                      register_button.setText("Checking Status...");
                      register_button.setVisibility(View.VISIBLE);
                    }

                    loadToast.success();
                  }

                } catch (JSONException e) {
                  e.printStackTrace();
                  loadToast.error();
                }
              }
            },
            new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                loadToast.error();
                error.printStackTrace();

                text.setText("Details about event is currently unavailable.");
                error_image.setVisibility(View.VISIBLE);
              }
            });

    jsonObjectRequest.setRetryPolicy(
        new DefaultRetryPolicy(10000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    MySingleton.getInstance(MyApplication.getAppContext()).addToRequestQueue(jsonObjectRequest);
  }
示例#4
0
  private void getRequest() {

    loadToast = new LoadToast(Profile.this);
    loadToast.setText("LOADING");
    loadToast.setTranslationY((int) Utils.convertDpToPixel(70, Profile.this));
    loadToast.show();

    final JsonObjectRequest jsonObjectRequest =
        new JsonObjectRequest(
            getURL(),
            new Response.Listener<JSONObject>() {
              @Override
              public void onResponse(JSONObject response) {
                Log.d("RESPONSE-PROFILE", response.toString());
                try {
                  loadToast.success();
                  JSONObject data = response.getJSONObject("data");
                  String status = response.getString("status");

                  if (status.equals("User Profile Info")) {
                    String email = data.getString("email");
                    String name = data.getString("name");
                    int silver_coins = Integer.parseInt(data.getString("silver_coins"));
                    int gold_coins = Integer.parseInt(data.getString("gold_coins"));
                    user = new User();
                    user.setEmail(email);
                    user.setName(name);
                    user.setGoldCoins(gold_coins);
                    user.setSilverCoins(silver_coins);
                    email_textview.setText(email);
                    name_textview.setText(name);
                    silverCoins_textview.setText(Integer.toString(silver_coins));
                    goldCoins_textview.setText(Integer.toString(gold_coins));
                    String roll = null;

                    if (data.has("rollno")) {
                      if (!data.isNull("rollno")) {
                        roll = data.getString("rollno");
                        username_textview.setText(roll);
                        user.setRoll(roll);
                      } else {
                        username_textview.setVisibility(View.GONE);
                      }
                    }
                  }
                } catch (JSONException e) {
                  e.printStackTrace();
                }
              }
            },
            new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                loadToast.error();
              }
            }) {
          @Override
          public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> map = new HashMap<>();
            map.put("Authorization", "bearer " + new PersonalData(Profile.this).getToken());
            return map;
          }
        };

    jsonObjectRequest.setRetryPolicy(
        new DefaultRetryPolicy(10000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    MySingleton.getInstance(MyApplication.getAppContext()).addToRequestQueue(jsonObjectRequest);
  }