Home » Android » android – How do I access response headers while using RxJava2 and Retrofit2?

android – How do I access response headers while using RxJava2 and Retrofit2?

Posted by: admin June 15, 2020 Leave a comment

Questions:

The Premise

I’m working on a simple app where I want to list out a user’s GitHub repos in a RecyclerView. I’m using this as my endpoint while building this.

The Problem

The problem I’m facing is that the GitHub API returns only 30 repos in one go. To get more, I can append a per_page=100 (100 is the maximum) to my query string; But, what do we do about users with more than 100 repos?

The solution the API docs provide is to get the next; url from the “Link” response header to make a second API call.

How does one go about this? Thanks!

How to&Answers:

The Response class will let you access the headers. The GitHub client would have a method like:

@GET("/search/code")
Observable<Response<T>> getUser(...)

Inside onNext(), you’d do something like:

@Override
public void onNext(Response<T> response) {
    String next = response.headers().get("next");
}