首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新2张贴与每个关键?

更新2张贴与每个关键?
EN

Stack Overflow用户
提问于 2017-05-09 02:10:16
回答 1查看 1.8K关注 0票数 1

这是我的界面:

代码语言:javascript
复制
public interface ApiInterface {
@GET("solicitation/all")
Call<SolicitationResponse> getAllNews(@Query("X-Authorization") String apiKey);

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

这是创建一个新的招标的MainActivity代码:

代码语言:javascript
复制
    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键。@Query("X-Authorization")

似乎我不能对“身体”做同样的事。

有没有一种方法可以像在查询中那样插入api密钥?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-09 02:26:37

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

代码语言:javascript
复制
Call<Solicitation> createSolicitation(@Query("X-Authorization") String apiKey, @Body Solicitation solicitation);

或在标题中

代码语言:javascript
复制
Call<Solicitation> createSolicitation(@Header("X-Authorization") String apiKey, @Body Solicitation solicitation);

或者您需要一个拦截器来插入报头。

代码语言:javascript
复制
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();  

用法

代码语言:javascript
复制
Call<Solicitation> call = apiService.createSolicitation("YOUR API KEY",solicitation);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43859927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档