Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add header to Google Cloud Endpoint in Android

I have the following approach for getting instance of my endpoint. I want to add a header to it. How do I do that? Please modify my code following, to include the header bit. Thanks.

public class RemoteServiceEndpointReference {
    private static final boolean USING_LOCAL_SERVER = false;
    private static final String LOCAL_SERVER_PATH = “…”;

    private static RemoteService service;

    public static RemoteService getRemoteServiceEndpoint() {
        if (null != service) {
            return service;
        }

        RemoteService.Builder builder = new RemoteService.Builder(
                AndroidHttp.newCompatibleTransport(),
                new AndroidJsonFactory(),
                null
        );
        forLocalServer(builder);
        service = builder.build();
        return service;
    }

    private static void forLocalServer(AbstractGoogleJsonClient.Builder builder) {
        if (USING_LOCAL_SERVER) {
            builder.setRootUrl(LOCAL_SERVER_PATH)
                   .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                       @Override
                       public void initialize(AbstractGoogleClientRequest<?> request) throws IOException {
                           request.setDisableGZipContent(true);
                       }
                   });
        }
    }
}
like image 207
Katedral Pillon Avatar asked Nov 20 '25 19:11

Katedral Pillon


1 Answers

You should be able to do something like this:

@Override
public void initialize(AbstractGoogleClientRequest<?> request) throws IOException {
    HttpHeaders yourHeaders = new HttpHeaders();
    header.set("yourHeader", value);
    // ...

    request.setDisableGZipContent(true);
    request.setRequestHeaders(yourHeaders); // setting the headers
}
like image 101
earthw0rmjim Avatar answered Nov 22 '25 07:11

earthw0rmjim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!