gpt4 book ai didi

android - 使用 Retrofit 2 向请求添加 header

转载 作者:行者123 更新时间:2023-11-29 02:36:23 29 4
gpt4 key购买 nike

我正在尝试发送带有身份验证 header 的请求,但服务器似乎无法识别客户端。我用了this教程,并实现了一个拦截器,如下所示:

public class AuthenticationInterceptor implements Interceptor {

private String authId;
private String authToken;

public AuthenticationInterceptor(String authId, String authToken) {
this.authId = authId;
this.authToken = authToken;
}

@Override
public Response intercept(@NonNull Chain chain) throws IOException {
Request original = chain.request();
Request.Builder builder = original.newBuilder();

if (authId != null && authToken != null) {
Timber.d("adding auth headers for: " + this);
builder.header("auth_id", authId)
.header("auth_token", authToken);
}

Request request = builder.build();
return chain.proceed(request);
}
}

当我尝试向服务器发送经过身份验证的请求时,它返回错误响应 409。服务器人员告诉我我缺少那些参数:(例如,由 Postman 接收)

    “accept”: [
“*/*”
],
“accept-encoding”: [
“gzip, deflate”
],
“cookie”: [
“PHPSESSID=ah1i1856bkdln5pgmsgjsjtar3"
]
  • 我认为使用 Dagger2 可能会导致此问题(请参阅 here ),所以我隔离了 okHttpClient,但它仍然不起作用。

  • 这是我的用法实现(非常简单):

    Retrofit retrofit;
    OkHttpClient client;
    AuthenticationInterceptor authenticationInterceptor;
    HttpLoggingInterceptor loggingInterceptor;

    private void testHeaders() {
    loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
    @Override
    public void log(String message) {
    Timber.i(message);
    }
    });
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

    client = new OkHttpClient.Builder()
    .addInterceptor(loggingInterceptor)
    .build();

    retrofit = new Retrofit.Builder()
    .addConverterFactory(GsonConverterFactory.create())
    .client(client)
    .baseUrl(BuildConfig.SERVER_ADDRESS)
    .build();

    retrofit.create(EntrOnline.class).getLoginToken("email@email.com", "XXX").enqueue(new Callback<NewAccount>() {
    @Override
    public void onResponse(Call<NewAccount> call, Response<NewAccount> response) {

    authenticationInterceptor = new AuthenticationInterceptor(response.body().getAuthId(), response.body().getAuthToken());

    client = new OkHttpClient.Builder()
    .addInterceptor(loggingInterceptor)
    .addInterceptor(authenticationInterceptor)
    .build();

    retrofit = new Retrofit.Builder()
    .addConverterFactory(GsonConverterFactory.create())
    .client(client)
    .baseUrl(BuildConfig.SERVER_ADDRESS)
    .build();

    retrofit.create(EntrOnline.class).getKeys("50022d8a-b309-11e7-a902-0ac451eb0490").enqueue(new Callback<List<NewEkey>>() {
    @Override
    public void onResponse(Call<List<NewEkey>> call, Response<List<NewEkey>> response) {
    Timber.d("Test Api");
    }

    @Override
    public void onFailure(Call<List<NewEkey>> call, Throwable t) {

    }
    });

    }

    @Override
    public void onFailure(Call<NewAccount> call, Throwable t) {

    }
    });

谢谢!

最佳答案

@Headers("Accept: application/json")
@FormUrlEncoded
@POST("sendWalletMoney")
Call<WalletResponse> sendWalletMoney(@Field("sender") String sender,
@Field("receiver") String receiver,
@Field("amount") String amount,
@Field("trnsx_type") String trnsx_type,
@Field("currency") String currency,
@Field("module_name") String module_name);

关于android - 使用 Retrofit 2 向请求添加 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46791074/

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