CelestialCreator CelestialCreator - 1 year ago 68
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();

protected void onCreate(Bundle savedInstanceState) {
latitude = (TextView) findViewById(R.id.Coordinates);
longitude = (TextView) findViewById(R.id.longitude);

new retrievedata().execute();

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

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


return lat;

protected void onPostExecute(String lat) {




Answer Source

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> {

    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

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

        return null;

    protected void onProgressUpdate(String... values) {


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;

    protected void onResume() {
        retrieveData = new RetrieveData();

    protected void onPause() {
        retrieveData = null;

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download