Paldan
Paldan

Reputation: 435

AsyncTask loop in Android

I am writing a program which gets currency price from the internet and I put this code in an asynctask. I need to be able to get constant updates but I'm not too sure loop asynctask without messing up the program. Any help appreciated!

I need this code to run continuously:

public void parseJSON(){
        new AsyncTask<Void, Void, String>() {
            protected String doInBackground(Void... params) {

                try {
                    return downloadJSON(ticker);
                } catch (Exception e) {
                    Log.d("JWP", e.toString());
                }

                return "Can't reach server. Is Internet access enabled?";
            }

            @Override
            protected void onProgressUpdate(Void... values) {
                TextView textView = (TextView) findViewById(R.id.ltcdata);

                textView.setText("Retrieving Data...");
            }

            @Override
            protected void onPostExecute(String result) {
                TextView textView = (TextView) findViewById(R.id.ltcdata);

                try {
                    JSONObject items = (JSONObject) new JSONTokener(result)
                            .nextValue();

                    String price = items.getJSONObject("ticker").getString(
                            "last");
                    String high = items.getJSONObject("ticker").getString(
                            "high");
                    String low = items.getJSONObject("ticker").getString("low");

                    textView.append("Last Price: $" + price + "\n");
                    textView.append("High: $" + high + "\n");
                    textView.append("Low: $" + low + "\n");

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        }.execute();
    }

Upvotes: 2

Views: 3030

Answers (4)

Lazy Ninja
Lazy Ninja

Reputation: 22537

Use a handler for the timer. In the following example it will check continuously every 5 seconds.

private Handler handler = new Handler();


private Runnable runnable = new Runnable() {

 public void run() {

  parseJSON();

  /*
   * Now register it for running next time
   */

  handler.postDelayed(this, 5000); // the request will be made again 5 sec later
 }


};

Use runnable.run(); to start the timer and runnable.removeCallbacks(handler ); to stop it.

Upvotes: 1

SweetWisher ツ
SweetWisher ツ

Reputation: 7306

Use thread which executes at specified time interval and call this async task in that thread.

Upvotes: 0

Nambi
Nambi

Reputation: 12042

create the function that call your asynctask function and inside the asynctask function call the function that call your asynctask.

calling your asynctask();

public void callasynctask(){
    parseJSON();
    }

inside the parseJSON();

public void parseJSON(){

.
.
.
.
.
.
  }

        }.execute();
callasynctask()
}

Upvotes: 0

ilango j
ilango j

Reputation: 6037

User timer task to execute the async task with a specific interval

Android Timer task

Upvotes: 0

Related Questions