Search code examples
androidhttp-getandroid-annotations

How exactly should I add header to HTTP Get request with AndroidAnnotations?


I am using approach, described in this question to add my specific header to HTTP get request. But I can't understand how I need to change my code to make interceptor to do his job. Currently I am using something like this:

@RestService
ImwizardClient imwizardClient;

//some code

return  imwizardClient.getAllCategories();

where getAllCategories() is method, which makes get request. The request works correctly, but it doesn't add my custom header. So what do I need to change?


Solution

  • Is your Interceptor defined for your RestService class as documented here?

    @Rest(interceptors = { HttpBasicAuthenticatorInterceptor.class })
    public interface ImwizardClient {
     // ... snipped
    }
    

    Alternatively, the workaround posted in this thread seems to work reliably. Just define a custom MessageConverter for your RestService class.

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

    }