我应该如何使用AndroidAnnotations将标头添加到HTTP Get请求?

时间:2013-12-20 10:23:18

标签: android http-get android-annotations

我正在使用this question中描述的方法将我的特定标头添加到HTTP获取请求。但我无法理解我需要如何更改代码以使拦截器完成其工作。目前我正在使用这样的东西:

@RestService
ImwizardClient imwizardClient;

//some code

return  imwizardClient.getAllCategories();

其中 getAllCategories()是方法,它产生get请求。请求正常,但它不会添加我的自定义标头。那么我需要改变什么?

1 个答案:

答案 0 :(得分:1)

您的Interceptor是否已根据文档here为您的RestService类定义?

@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);
    }

}