How to load large number of json data using volley

1.2k views Asked by At

I am using this Volley example

It works perfect, but when I change the response of data to 150+ records from server it takes a long time to load.

So I want to load small amount of data 5-10 record then another 5-10 records.

How can I do this with Volley?

2

There are 2 answers

0
Aurasphere On

You could use AsyncTask to do something like this:

private class DatabaseTask extends AsyncTask<URL, Integer, String> {
 protected String doInBackground(URL... urls) {

    //put here your volley query.

     }
     return results;
 }

 protected void onProgressUpdate(Integer... progress) {

     //You can show the progress of the download with this function:
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(String results) {
   //Parse the results here and show them in the UI.
 }
}

Then, assuming you are trying to connect to an online database, for example a MySQL database, you could easily implement a PHP interface to your database which returns only the amount of records you want (for example 10) and call the DatabaseTask whenever you need the records. Alternatively, you could do a separate Thread working on a timer which sends query whenever the timer fires.

1
Prathmesh Swaroop On

Volley will fetch all the response which it is supposed to receive at client end(Mobile end). This type of behavior cannot be accomplished on Client Side or Mobile end.

This should be done in the API or server side by implementing pagination in services, something like below:

http://serviceendpoint.com?start=0&&end=5 fetch first five record.
http://serviceendpoint.com?start=5&&end=10 fetch next five record.

//Constant Variable denoting after how many  
//to call in one service
//items numbers you need

private final int LIST_FETCH_COUNT =5;
private final int TOTAL_COUNT=1500;
private int start=0;
private int end=0;

//you can do something like this

for(int i=0;i<TOTAL_COUNT;i+=LIST_FETCH_COUNT){
    callAsyncService(i,i+LIST_FETCH_COUNT);
}

private void callAsyncService(int start,int end){

    //create a new async object
    asynObject.execute(Void,Void,start,end);
}

//inside your async

doInBackground(params...){

    //get start and end values from params and run a service
}