3

我想使用自定义标头发出 POST 请求。我找不到如何使用 AA Rest API 执行此操作的信息 - https://github.com/excilys/androidannotations/wiki/Rest%20API

我应该使用用于经过身份验证的请求的 ClientHttpRequestInterceptor 吗? https://github.com/excilys/androidannotations/wiki/Authenticated-Rest-Client

谢谢你的帮助!

4

2 回答 2

4

目前有一个未解决的问题:https ://github.com/excilys/androidannotations/issues/323

目前,唯一的方法是使用自定义 ClientHttpRequestInterceptor。这是一个小例子:

@EBean
public class CustomHeaderInterceptor implements ClientHttpRequestInterceptor {

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] data, ClientHttpRequestExecution execution) throws IOException {
        request.getHeaders().add("myHeader", "value");
        return execution.execute(request, data);
    }

}

然后,您需要将其链接到 restTemplate,如下所示:

@EBean
public class MyService {

    @RestService
    RestClient restClient;

    @Bean
    MobileParametersInterceptor mobileParametersInterceptor;

    @AfterInject
    public void init() {
        List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(mobileParametersInterceptor);
        restClient.getRestTemplate().setInterceptors(interceptors);
    }

}
于 2012-09-28T08:50:50.247 回答
1

实际上,您必须将 ClientHttpRequestInterceptor 用于自定义标头。目前,这是我知道的唯一方法。

有关 RestTemplate 的更多信息,请参阅 Spring-Android 的官方文档

于 2012-09-28T09:01:55.403 回答