gpt4 book ai didi

android - 改造 2 使用 apikey 发布?

转载 作者:行者123 更新时间:2023-11-29 15:39:17 25 4
gpt4 key购买 nike

这是我的界面:

public interface ApiInterface {
@GET("solicitation/all")
Call<SolicitationResponse> getAllNews(@Query("X-Authorization") String apiKey);

@POST("solicitation/create ")
Call<Solicitation> createSolicitation(@Body Solicitation solicitation);
}

这是创建新请求的 MainActivity 代码:

    Solicitation solicitation = new Solicitation("xx", "list", "31", "32", "description goes here", "file goes here", "userid goes here", "203120312");

ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

Call<Solicitation> call = apiService.createSolicitation(solicitation);
call.enqueue(new Callback<Solicitation>() {
@Override
public void onResponse(Call<Solicitation> call, Response<Solicitation> response) {
Log.d("Response::", "Success!");
}

@Override
public void onFailure(Call<Solicitation> call, Throwable t) {
Log.e("Response::", "Fail!!");
}
});

问题是,正如您在上面的查询中看到的那样,我使用了 api key 。 @Query("X-Authorization")

看来我不能对@Body 做同样的事情。

有没有办法像在查询中那样插入 api key ?

最佳答案

只需添加以逗号分隔的查询

Call<Solicitation> createSolicitation(@Query("X-Authorization") String apiKey, @Body Solicitation solicitation);

或在标题中

Call<Solicitation> createSolicitation(@Header("X-Authorization") String apiKey, @Body Solicitation solicitation);

或者你需要一个拦截器来插入header

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();  
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();

// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.header("X-Authorization", "YOUR AUTH KEY"); // <-- this is the important line

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

OkHttpClient client = httpClient.build();

用法

Call<Solicitation> call = apiService.createSolicitation("YOUR API KEY",solicitation);

关于android - 改造 2 使用 apikey 发布?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43859927/

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