我使用的是this Volley示例
它工作得很完美,但当我将数据的响应更改为来自服务器的150+记录时,需要很长时间才能加载。
所以我想加载少量的数据5-10条记录,然后再加载5-10条记录。
我怎样才能用Volley做到这一点?
发布于 2015-06-11 20:54:53
Volley将获取它应该在客户端(移动端)接收的所有响应。这种类型的行为不能在客户端或移动端完成。
这应该在API或server side中通过在服务中实现pagination来完成,如下所示:
http://serviceendpoint.com?start=0&&end=5获取前五条记录。
http://serviceendpoint.com?start=5&&end=10获取下一个5条记录。
//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
}发布于 2015-06-11 21:15:03
你可以使用AsyncTask来做类似这样的事情:
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.
}
}然后,假设您正在尝试连接到在线数据库,例如MySQL数据库,您可以很容易地实现数据库的PHP接口,该接口只返回所需的记录量(例如10),并在需要记录时调用DatabaseTask。或者,您可以在计时器上执行一个单独的线程,该计时器在计时器触发时发送查询。
https://stackoverflow.com/questions/30780859
复制相似问题