1 year ago

#381308

test-img

Aeternis

My current code takes a JSON response, parses it, then displays the value to an activity, all in one function. How do I separate them into threads?

(note: I'm using the Android Volley library for the network connection)

public class PostureActivity extends AppCompatActivity {
private static final String LOG_TAG = PostureActivity.class.getName();

private static final String EMB_URL = "https://api.thingspeak.com/channels/xxxxxxx/feed/last.json?round=1";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
protected void onResume() {
    super.onResume();
    connect(); // call at the start

    final Handler handler = new Handler();
    Runnable scrape = new Runnable() {
        @Override
        public void run() {
            connect(); // call every x ms
            handler.postDelayed(this, 3000);
        }
    };
    handler.postDelayed(scrape, 3000);
}


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());

                    // TODO: cast to double to show the average
                    String ultrasonic = myResponse.getString("field1");
                    String flex1 = myResponse.getString("field2");
                    String flex2 = myResponse.getString("field3");
                  
                    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(flex1);
                    butt.setText(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);
}

As you can see, connect() essentially retrieves, parses, and displays the data, and I run it via a handler. How do split the code so that this entire function doesn't simply populate the UI thread? I'm not very familiar with handler/loopers or java threads outside of async tasks, so I was hoping that I could be pointed in the right direction as to how to optimize the function better.

java

android

android-volley

0 Answers

Your Answer

Accepted video resources