【问题标题】:How exactly should I add header to HTTP Get request with AndroidAnnotations?我应该如何使用 AndroidAnnotations 将标头添加到 HTTP Get 请求?
【发布时间】:2013-12-20 10:23:17
【问题描述】:
我正在使用this question 中描述的方法将我的特定标头添加到HTTP 获取请求。但我不明白我需要如何更改我的代码以使拦截器完成他的工作。目前我正在使用这样的东西:
@RestService
ImwizardClient imwizardClient;
//some code
return imwizardClient.getAllCategories();
其中 getAllCategories() 是方法,它发出获取请求。该请求正常工作,但它没有添加我的自定义标头。那么我需要改变什么?
【问题讨论】:
标签:
android
http-get
android-annotations
【解决方案1】:
您的 Interceptor 是否为您的 RestService 类定义为记录的here?
@Rest(interceptors = { HttpBasicAuthenticatorInterceptor.class })
public interface ImwizardClient {
// ... snipped
}
另外,this thread 中发布的解决方法似乎可以可靠地工作。只需为您的 RestService 类定义一个自定义 MessageConverter。
public class GsonWithHeadersConverter extends GsonHttpMessageConverter {
@Override
protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
setHeaders(outputMessage); //My method to put the additional headers :)
super.writeInternal(o, outputMessage);
}
}