Advertisement
Guest User

Untitled

a guest
Apr 5th, 2022
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. private void connect() {
  2.         MySingleton.getInstance(this.getApplicationContext()).getRequestQueue();
  3.  
  4.         JsonObjectRequest collectData = new JsonObjectRequest(
  5.                 Request.Method.GET, // HTTP method
  6.                 EMB_URL, // URL string that returns the desired JSON TODO: change appropriate url
  7.                 null, // optional JSON to send as request
  8.                 response -> { // retrieved data
  9.                     try {
  10.                         JSONObject myResponse = new JSONObject(response.toString());
  11.                         JSONArray currentResponse = myResponse.getJSONArray("feeds");
  12.  
  13.                         // 0 always gets the latest
  14.                         JSONObject currentJSON = currentResponse.getJSONObject(0);
  15.  
  16.                         // TODO: cast to double to show the average
  17.                         String ultrasonic = currentJSON.getString("field1");
  18.                         String flex1 = currentJSON.getString("field2");
  19.                         String flex2 = currentJSON.getString("field3");
  20.                         // String pressure1 = currentJSON.getString("field4");
  21.                         // String pressure2 = currentJSON.getString("field5");
  22.  
  23.                         TextView neck = findViewById(R.id.neck_number);
  24.                         TextView back = findViewById(R.id.back_number);
  25.                         // TextView butt = findViewById(R.id.butt_number);
  26.  
  27.                         neck.setText(ultrasonic);
  28.                         back.setText(converter(flex1, flex2));
  29.  
  30.                     } catch (JSONException e) { // what if response is null?
  31.                         //
  32.                         e.printStackTrace();
  33.                         Toast.makeText(getApplicationContext(), "Response values are empty.", Toast.LENGTH_LONG).show();
  34.                         finishAffinity();
  35.                         finishAndRemoveTask();
  36.                     }
  37.                 },
  38.                 error -> { // retrieved error/failure
  39.                     error.printStackTrace();
  40.                     Toast.makeText(getApplicationContext(), "Could not connect to website.", Toast.LENGTH_LONG).show();
  41.                     finishAffinity();
  42.                     finishAndRemoveTask();
  43.                 }
  44.         );
  45.         MySingleton.getInstance(this).addToRequestQueue(collectData);
  46.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement