gpt4 book ai didi

http-headers - Retrofit 2.0 header 身份验证

转载 作者:行者123 更新时间:2023-12-01 17:58:38 25 4
gpt4 key购买 nike

private void setUpRestClient() {
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.header("Accept", "application/pyur.v1")
.header("Authorization", new SharedPreferencesUtil(getBaseContext()).getToken())
.header("Content-Type", "application/json")
.method(original.method(),original.body())
.build();
return chain.proceed(request);
}
});
RestClient.getInstance().configureRestAdapter(this, getResources().getString(R.string.base_url),client);
}

public void configureRestAdapter(final Context context, String baseUrl, OkHttpClient client) {
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
.excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build();
service = retrofit.create(NetworkServiceInterface.class);
}

这现在在 Retrofit 2.0 中给了我一个失败返回,最初我没有“Authorization” header ,它给了我未经授权的权限,这是可以理解的。但现在我用我的身份验证 token 对其进行授权,但失败了。刚接触 Retrofit 2.0,谢谢 --

最佳答案

您可以将授权 header 传递为:

@GET("/v1/OrderReport.json")
Call<POJO_Class> getExampleMethod(@Header("Authorization") String token, @Query("id") String id);

然后调用:

getExampleMethod("Basic " + token, id);

关于http-headers - Retrofit 2.0 header 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34821958/

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