Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void connect() {
- MySingleton.getInstance(this.getApplicationContext()).getRequestQueue();
- JsonObjectRequest collectData = new JsonObjectRequest(
- Request.Method.GET, // HTTP method
- EMB_URL, // URL string that returns the desired JSON TODO: change appropriate url
- null, // optional JSON to send as request
- response -> { // retrieved data
- try {
- JSONObject myResponse = new JSONObject(response.toString());
- JSONArray currentResponse = myResponse.getJSONArray("feeds");
- // 0 always gets the latest
- JSONObject currentJSON = currentResponse.getJSONObject(0);
- // TODO: cast to double to show the average
- String ultrasonic = currentJSON.getString("field1");
- String flex1 = currentJSON.getString("field2");
- String flex2 = currentJSON.getString("field3");
- // String pressure1 = currentJSON.getString("field4");
- // String pressure2 = currentJSON.getString("field5");
- TextView neck = findViewById(R.id.neck_number);
- TextView back = findViewById(R.id.back_number);
- // TextView butt = findViewById(R.id.butt_number);
- neck.setText(ultrasonic);
- back.setText(converter(flex1, flex2));
- } catch (JSONException e) { // what if response is null?
- //
- e.printStackTrace();
- Toast.makeText(getApplicationContext(), "Response values are empty.", Toast.LENGTH_LONG).show();
- finishAffinity();
- finishAndRemoveTask();
- }
- },
- error -> { // retrieved error/failure
- error.printStackTrace();
- Toast.makeText(getApplicationContext(), "Could not connect to website.", Toast.LENGTH_LONG).show();
- finishAffinity();
- finishAndRemoveTask();
- }
- );
- MySingleton.getInstance(this).addToRequestQueue(collectData);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement