java - 移动端接口的设计问题
高洛峰
高洛峰 2017-04-18 09:54:14
0
5
1028

现有学生实体类 Student

public class Student {
    private int no;
    private String name;
    private int score;
}

需要为移动端提供数据查看学生成绩,数据源未使用数据库仅使用Java集合,若Server的数据源为List<Student>,内容如下:

0 张三 58
1 李四 42
2 王五 23

假设移动端每次请求只显示两条数据,则首次显示

0 张三 58
1 李四 42

移动端下拉页面,请求下一页数据,并将no=1发回给Server,此时移动端就多了一条数据,变为:

0 张三 58
1 李四 42
2 王五 23 --下拉后得到的新数据

若数据源固定不变这样是没有问题的,但实际情况是学生的信息会随时变化,即可能在移动端下拉请求下一页之前"1 李四 42"这条数据就没有了,此时再将no=1发回给Server会产生各种各样的问题,例如:同一条数据显示两次、有的未被显示等问题,有什么方法在不使用数据库的情况下按这种方式为移动端提供列表数据呢?想不通哇=。=

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(5)
伊谢尔伦

Have you not done mobile paging yet? ? When mobile paging involves changing data, it must not be based on page=1, otherwise it will be repeated or missed. (It’s okay to miss it, you will find it after repeating it on the client)
The general solution is as follows:
First of all, you must have a sorting, such as by time, score, etc. If the client wants to go to the next page, it only needs to pass the identifier of the last record of the current page. The server will get this identifier and search for the next one. of data to it.
Example:
Suppose the server data is 1,2,3,4,5,6,7,8 and the data returned to the client is 1,2,3. If the data after 3 is needed, 3 will be sent back to the server. The server gets 3 and then according to the agreed logic, it finds 4, 5, 6 which are larger than 3 and gives them to the client. In this way, even if it is added or deleted, there will be no duplication problem. In the past, this is how we did waterfall flow picture display. In addition, FB, instagram Many APIs are designed like this

洪涛

no=1 If you have any, look for no=2

迷茫

You don’t use a database, so why does the data disappear?
Is paging possible? According to the list you saved. Similar to the following pseudo code:
`
List<x> list1 = list.subList(0,3);
List&lt ;x> list2 = list.subList(3,6);
List<x> list3 = list.subList(6,9);
`

阿神

It is really troublesome when the data in the paginated list is deleted...

There are 3 solutions to consider:

  1. If the amount of data is small, don’t do paging, just give all the data in the list to the client at once

  2. If the amount of data is large and it is necessary to paginate, you can consider not only passing the data of the page you want to obtain each time, but also passing the diff (changes) of the data of the page that has been obtained previously to the client

  3. If the data changes, send a flag indicating that the data has changed to the client, allowing the client to refresh the entire list or prompt the user to refresh the entire list.

伊谢尔伦

First sort the collection of students by id, and then use the id and size of the last data in the previous request as the input parameters of the request for paging. The id here is Student.no.

for(Student student:students){
  if(student.no>lastId){
      result.add(student);
      if(result.size()==size){
          return result;
      }
  }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template