Retrofit (1-2) 中的动态 headers 不起作用

Dinamic headers in Retrofit (1-2) do not work

我正在尝试向我的服务器发出 authtoken GET 请求。 我正在尝试这样做:

public interface FixedRecApi {


    public static final String ENDPOINT = "http://******.pythonanywhere.com/";

    //@Headers("Authorization: Token ce7950e8d0c266986b7f972407db898810322***") this thing work well!!
    @GET("/auth/me/")
    Observable<User> me(@Header("Authorization: Token") String token); //this does not work at all!
    Observable<User> me();


}

如您所见,带有显式 header 的行:@Headers - 完美无缺。 但是当我尝试将它作为参数传递时 - 它说 "no credentials provided"。 我的申请 onCreate:

@Override
    public void onCreate() {
        super.onCreate();
        ActiveAndroid.initialize(this);

        Retrofit retrofit = new Retrofit.Builder()

                .baseUrl(FixedRecApi.ENDPOINT)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(
                        GsonConverterFactory.create(new GsonBuilder()
                                .excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
                                .excludeFieldsWithoutExposeAnnotation()
                                .serializeNulls()
                .create()))
                .build();

        service = retrofit.create(FixedRecApi.class);

    }

不知道这东西有什么问题。拦截器也不起作用...

我找到了解决方案。 Headers 由两部分组成:

  1. header 姓名:"Authrorization"
  2. 然后冒号
  3. header 值:"Token ce7950e8d0c266986b7f972407db898810322***"

所以,Retrofit 的用法应该是:

Observable<User> me(@Header("Authorization") String token);

然后例如在 MainActivity 中:

RetrofitApi.me("Token " + "ce7950e8d0c266986b7f972407db898810322***");