【问题标题】:How do I access response headers while using RxJava2 and Retrofit2?使用 RxJava2 和 Retrofit2 时如何访问响应头?
【发布时间】:2018-03-05 05:28:29
【问题描述】:

前提

我正在开发一个简单的应用程序,我想在 RecyclerView 中列出用户的 GitHub 存储库。我在构建它时使用this 作为我的端点。

问题

我面临的问题是 GitHub API 一次只返回 30 个 repos。要获得更多信息,我可以将per_page=100(最大值为 100)附加到我的查询字符串中; 但是,对于拥有超过 100 个 repo 的用户,我们该怎么办?

API docs 提供的解决方案是从“Link”响应标头中获取 next; url 以进行第二次 API 调用。

如何解决这个问题?谢谢!

【问题讨论】:

    标签: android retrofit2 github-api rx-java2 clean-architecture


    【解决方案1】:

    Response 类将允许您访问标题。 GitHub 客户端会有一个类似的方法:

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

    onNext() 中,您可以执行以下操作:

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

    【讨论】:

    • Observable> 告诉我 Type 'okhttp3.Response' does not have type parameters. 因此,我尝试使用 Observable 进行调用,这导致了 'okhttp3.Response' is not a valid response body type. Did you mean ResponseBody? 错误。想法?
    • 您需要Response class in Retrofit。不行http。
    猜你喜欢
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    相关资源
    最近更新 更多