简体   繁体   English

如何从 github 搜索 api 中获取所有结果?

[英]How get all results from github search api?

I need to get all results with github search api with pagination using.我需要使用 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. 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 But I don't understand how can I to implement transition to the next page using these links. 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但我不明白如何使用这些链接实现到下一页的转换。

You can create a variable to keep track of your current page.您可以创建一个变量来跟踪当前页面。 & increment it on the success each time until your list size becomes equal to total_count . & 每次成功都会增加它,直到您的列表大小等于total_count

on RecyclerView end reached method can be called again.可以再次调用RecyclerView结束到达的方法。

I have tried to demonstrate the example as following please use the actual calls & stuff that you're using.我试图演示以下示例,请使用您正在使用的实际调用和内容。 just understand the pattern.只需了解模式。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM