[英]How get all results from github search api?
我需要使用 github 搜索 api 并使用分页来获取所有结果。 Now I use request: https://api.github.com/search/repositories?q=lib&page=1&per_page=20
I read that the responses also includes the Link header which contains a ready-made URL to the next page. In response I have link https://api.github.com/search/repositories?q=lib&page=2&per_page=20>
and link to the last page: https://api.github.com/search/repositories?q=java&page=50&per_page=20
但我不明白如何使用这些链接实现到下一页的转换。
您可以创建一个变量来跟踪当前页面。 & 每次成功都会增加它,直到您的列表大小等于total_count
。
可以再次调用RecyclerView
结束到达的方法。
我试图演示以下示例,请使用您正在使用的实际调用和内容。 只需了解模式。
public class MainActivity extends AppCompatActivity {
private int page = 1;
private ArrayList<Object> items = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
call();
}
//dummy call
private void call(){
String url = "https://api.github.com/search/repositories?q=lib&page=" + page + "&per_page=20";
//pass the new url with page to call or just params as per your need
new ResponseListener(url){
@Override
public void onSuccess(ArrayList<Object> list,Integer totalCount) {
//parse & add items
items.addAll(list);
//check if you have items same as total count.
// If true then it means you've reached the end
if (items.size() < totalCount){
//increase the page number for next call
page++;
}
}
@Override
public void onError(Exception e) {
e.printStackTrace();
}
};
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.