dougu5847 2016-12-24 14:08
浏览 37
已采纳

当用户在app中滚动时,从服务器发送JSON

I am making a wallpaper app for android. And also building API on my private server for it. My problem is what should be done on the server side so that only those image's JSON is sent which are required. That is, on the app screen, if 10 grids are available then only 10 images are fetched and rest gets loaded when the user scrolls.

  • 写回答

1条回答 默认 最新

  • doucan957495 2016-12-24 14:17
    关注

    You can pass an additional parameter like page={{pageNumber}} and handle it in server side.

    For example if your api is example.com/api, then by default you should assume it contains page=0. You should choose a threshold of data per page. For example if you choose your threshold as 10, then the above api call should return first 10 images.

    In the app, you should listen to the scroll event and if the last visible item reached the end of data you are having, then call the api with page={{lastPageNumber+1}} and you should handle it in server side to return corresponding data for that page numbers.

    This is a rough implementation of what would be like in server side if you use mysql and php.

    <?php
    $page = 0;
    if (isset($_GET['page'])) {
        $page=$_GET['page'];
    }
    
    $threshold = 10;
    $offset = $threshold + ($threshold * $page);
    
    mysql_connect("domain", "username", "password")
                or die('Could not connect: ' . mysql_error());
        mysql_select_db("dbName") or die('Could not select database');
    
    
    $sql=" SELECT COLUMN_NAMES from TABLE_NAME limit $threshold offset $offset";
    
     $result = mysql_query($sql) or die('Query failed: ' . mysql_error());
    
     //process the $result
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。