How to add Authorization header with Retrofit2 + RxJava

时间:2016-07-11 20:44:58

标签: android retrofit retrofit2

I want to perform request using Retrofit2 and RxJava

public static Observable<Post> getPostsAround(Location location, int offset, int limit) {
    if(api==null) {
        new RestService();  //initialize API in constructor
    }
    return api.getPostsAround(location.getLatitude(),location.getLongitude(),offset,limit)
            .flatMapIterable(posts -> posts);   //transform Observable<List<Post>> to Observable<Post> which emits posts onNext
}

I tried @Headers("Authorization: code) annotation, but I don't know how to change "code" in runtime.

1 个答案:

答案 0 :(得分:10)

我找到了答案: 可以使用@Header注释动态更新请求标头。必须为@Header提供相应的参数。如果该值为null,则将省略标头。否则,将在值上调用toString,并使用结果。

@GET("user")
Call<User> getUser(@Header("Authorization") String authorization)
相关问题