Updating Content thru Volley at Time Intervals

snowman Source

I know there are multiple post on doing this, but with limited Android experience I am little confused as to who to believe. I have an app that loads content on start up from my server thru Volley request. After a period of time I want to make a Volley request back to update the content that is displayed to the user. When the app first loads, I determine the number of seconds from the half hour which I pass to the following

public static void refreshAllContent(final long timetoupdate) {
       new CountDownTimer(timetoupdate, 1000) {
           public void onTick(long millisUntilFinished) {
           }
           public void onFinish() {
             Log.i("SCROLLS ", "UPDATE CONTENT HERE ");
               resetContent();
           }
       }.start();
           }

On the finish it calls the refreshAllContent which is where I would make the Volley request and reset the count for the next update, I have something like

public static void resetContent(){
        Handler handler= new Handler();
        Runnable runnable= new Runnable() {
            @Override
            public void run() {
//PUT VOLLEY REQUEST HERE
                refreshAllContent(Times. initialTime());
            }
        };
    }

I guess I am stuck as to exactly how to make the Volley request, meaning what do I have to worry do this. Like I said not a lot of experience so not sure if I run the request in a special runnable or task. Any direction appreciated.

EDIT: I reworked this some, instead of going back to the refreshAllContent, I replaced this with

private static void resetContent(){
        Log.i("SCROLLS ", "ENTER resetContent");
         final Handler handler= new Handler();
         Runnable  runnableCode = new Runnable() {
            @Override
            public void run() {
               refreshData(); // Volley Request
               handler.postDelayed(runnableCode, 20000);
            }
        };
        handler.post(runnableCode);
   }

Logic is now on the initial run, the first timetoupdate is created and passed to the refreshAllContent. Once the countdown is complete, the resetContent() will run which makes the Volley Request in the refreshData(). Now I am getting an error stating the runnableCode needs to be declared final since it's accessed from an inner class, same for the handler. Well adding final to the

final Runnable runnableCode=new Runnable(){

line doesn't fix the error, I still have an error telling me the runnableCode has not been initialized. Can I get little help on this.

androidandroid-volleyrunnable

Answers

answered 6 months ago will #1

You don't create a Runnable to run Volly. Volly runs network calls on a background thread by default.

Here is a simple Volly code:

public void volleyProcess(){
    RequestQueue requestQueue = Volley.newRequestQueue(this);
        StringRequest request = new StringRequest(Request.Method.GET, "https://api.myjson.com/bins/753rt", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d(TAG, response);
            refreshAllContent(30000);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d(TAG, error.toString());
        }
    });
    requestQueue.add(request);
}

onResponse() is called when a response is successfully returned by the call. This method is called on the main thread therefore you can run your refreshAllContent() method here. and the parameter 'response' is the data returned, do what ever you want with it here(i am simply printing it to the Logcat).

Now to make this code run after the desired interval, just call it in onFinish() of the countdownTimer.

public static void refreshAllContent(final long timetoupdate) {
   new CountDownTimer(timetoupdate, 1000) {
       public void onTick(long millisUntilFinished) {
       }
       public void onFinish() {
         Log.i("SCROLLS ", "UPDATE CONTENT HERE ");
           volleyProcess();
       }
   }.start();
}

Hope this helped

comments powered by Disqus