CelestialCreator CelestialCreator - 1 month ago 12
JSON Question

How can you update the text views every two seconds With JSON?

I tried a Timer task but that doesn't help

I was trying to get the code to continuously get the and update Coordinates.
I v'e done research but none applies to what I'm using.
Would I put the onPostExecute inside the timer ?

I'm using a json OBJECT.

public class IsstatusActivity extends AppCompatActivity {

JSONParser jsonparser = new JSONParser();
TextView latitude;
TextView longitude;
String lat;
String longit;
JSONObject jobj = null;

private final ReentrantLock lock = new ReentrantLock();
private final Condition tryAgain = lock.newCondition();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.isstatus);
latitude = (TextView) findViewById(R.id.Coordinates);
longitude = (TextView) findViewById(R.id.longitude);


new retrievedata().execute();
}

class retrievedata extends AsyncTask<String, String, String> {


@Override
public String doInBackground(String... args) {

jobj = jsonparser.makeHttpRequest("http://api.wheretheiss.at/v1/satellites/25544");


try {
lat = "latitude : " + jobj.getString("latitude");
longit = "longitude : " + jobj.getString("longitude");
} catch (JSONException e) {
// TODO Auto-generated catch block


e.printStackTrace();
}



return lat;
}

protected void onPostExecute(String lat) {


latitude.setText(lat);
longitude.setText(longit);

}




}
}

Answer

I've seen by your questions over the past couple of days that you are struggling with this, so I will try to help out here.

Here's one way to do the timer part: AsyncTask has a means whereby you can report progress. When you call publishProgress within doInBackground, it will cause onProgressUpdate to be called on the UI thread. You can use these methods to do your updates.

// by convention class names begin with an uppercase letter in Java
class RetrieveData extends AsyncTask<Void, String, Void> {

    @Override
    protected Void doInBackground(Void... params) {

        try {

            while (! isCancelled()) {

                jobj = jsonparser.makeHttpRequest("http://api.wheretheiss.at/v1/satellites/25544");

                String lat = "latitude : " + jobj.getString("latitude");
                String long = "longitude : " + jobj.getString("longitude");

                // this will cause onProgressUpdate to be called with lat & long
                publishProgress(lat, long);

                // it's okay to sleep within the background thread
                Thread.sleep(2000);
            }

        } catch (InterruptedException e) {
            Log.w("RetrieveData", "thread was interrupted", e);
        } catch (JSONException e) {
            Log.e("RetrieveData", "parse error", e);
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(String... values) {
        latitude.setText(values[0]);
        longitude.setText(values[1]);
    }

}

Also, for best results I think you should have a reference to the task and manage it in your activity with onPause and onResume like this:

    private AsyncTask<Void, String, Void> retrieveData;

    @Override
    protected void onResume() {
        super.onResume();
        retrieveData = new RetrieveData();
        retrieveData.execute();
    }

    @Override
    protected void onPause() {
        retrieveData.cancel(true);
        retrieveData = null;
        super.onPause();
    }

This means you don't need to call task execute in onCreate.

This may not be the very best approach, but it's probably the simplest way just to get you started — until somebody else gives you a better answer.