首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用volley加载大量json数据

如何使用volley加载大量json数据
EN

Stack Overflow用户
提问于 2015-06-11 20:31:00
回答 2查看 1.2K关注 0票数 1

我使用的是this Volley示例

它工作得很完美,但当我将数据的响应更改为来自服务器的150+记录时,需要很长时间才能加载。

所以我想加载少量的数据5-10条记录,然后再加载5-10条记录。

我怎样才能用Volley做到这一点?

EN

回答 2

Stack Overflow用户

发布于 2015-06-11 20:54:53

Volley将获取它应该在客户端(移动端)接收的所有响应。这种类型的行为不能在客户端或移动端完成。

这应该在APIserver side中通过在服务中实现pagination来完成,如下所示:

http://serviceendpoint.com?start=0&&end=5获取前五条记录。

http://serviceendpoint.com?start=5&&end=10获取下一个5条记录。

代码语言:javascript
复制
//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
}
票数 1
EN

Stack Overflow用户

发布于 2015-06-11 21:15:03

你可以使用AsyncTask来做类似这样的事情:

代码语言:javascript
复制
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。或者,您可以在计时器上执行一个单独的线程,该计时器在计时器触发时发送查询。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30780859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档