gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:29:51 26 4
gpt4 key购买 nike

我正在尝试向我的服务器发出 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 - 工作完美。但是当我尝试将它作为参数传递时 - 它说“没有提供凭据”。我的申请 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);

}

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

最佳答案

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

  1. 标题名称:“授权”
  2. 然后冒号
  3. header 值:“ token ce7950e8d0c266986b7f972407db898810322***”

因此,Retrofit 的用法应该是:

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

然后例如在 MainActivity 中:

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

关于android - Retrofit (1-2) 中的动态 header 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35900929/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com