How to disable GZipContent in Cloud Endpoints builder in Android

喜欢而已 提交于 2019-12-04 11:41:30

问题


I want to disable GZipContent for a Google Cloud Endpoints class so that a POST can work on the local development server.

The latest GPE release generates this endpoint builder:

public static final class Builder extends AbstractGoogleJsonClient.Builder {
    public Builder(HttpTransport transport, JsonFactory jsonFactory,
        HttpRequestInitializer httpRequestInitializer) {
      super(
          transport,
          jsonFactory,
          ...);
    }

and Google documentation recommends using it like this:

Myendpoint.Builder endpointBuilder = new Myendpoint.Builder(
                    AndroidHttp.newCompatibleTransport(),
                    new GsonFactory(),
                    credential);

Does anyone know how to disable GZipContent for the endpoint?

Thanks.


回答1:


You can use:

builder.setGoogleClientRequestInitializer(new TictactoeRequestInitializer() {
     protected void initializeTictactoeRequest(TictactoeRequest<?> request) {
         request.setDisableGZipContent(true);
     }
   });

Replace TictactoeRequest with the appropriate class for your application.




回答2:


I'm not 100% why Dan's answer didn't work for me, but this GitHub project's code solved it for me.

/*
 * TODO: Need to change this to 'true' if you're running your backend locally using
 * the DevAppServer. See
 * http://developers.google.com/eclipse/docs/cloud_endpoints for more
 * information.
 */
protected static final boolean LOCAL_ANDROID_RUN = false;

/*
 * The root URL of where your DevAppServer is running (if you're running the
 * DevAppServer locally).
 */
protected static final String LOCAL_APP_ENGINE_SERVER_URL = "http://localhost:8080/";

/*
 * The root URL of where your DevAppServer is running when it's being
 * accessed via the Android emulator (if you're running the DevAppServer
 * locally). In this case, you're running behind Android's virtual router.
 * See
 * http://developer.android.com/tools/devices/emulator.html#networkaddresses
 * for more information.
 */
protected static final String LOCAL_APP_ENGINE_SERVER_URL_FOR_ANDROID = "http://10.0.2.2:8080";

/**
 * Updates the Google client builder to connect the appropriate server based
 * on whether LOCAL_ANDROID_RUN is true or false.
 *
 * @param builder Google client builder
 * @return same Google client builder
 */
public static <B extends AbstractGoogleClient.Builder> B updateBuilder(
        B builder) {
    if (LOCAL_ANDROID_RUN) {
        builder.setRootUrl(LOCAL_APP_ENGINE_SERVER_URL_FOR_ANDROID
                + "/_ah/api/");
    }

    // only enable GZip when connecting to remote server
    final boolean enableGZip = builder.getRootUrl().startsWith("https:");

    builder.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
        public void initialize(AbstractGoogleClientRequest<?> request)
                throws IOException {
            if (!enableGZip) {
                request.setDisableGZipContent(true);
            }
        }
    });

    return builder;
}



回答3:


For those that are Googling for the error I saw, it was java.io.EOFException, but only in the development server. Here's how I was able to fix this, using the example stated in the OP's question:

Myendpoint myendpointClient = new Myendpoint.Builder(
                AndroidHttp.newCompatibleTransport(),
                new GsonFactory(),
                credential).build();
EndpointService svcCall = myendpointClient.endpointService("firstArg");
// Note, I didn't call "execute()", as normal!
svcCall.setDisableGZipContent(true);
// This is also a handy place to set http headers, etc
svcCall.getRequestHeaders().set("x-oddballhdr","OddballValue");
// It's now time to call execute()
svcCall.execute();

That may be a bit simpler than the other helpful answers.



来源:https://stackoverflow.com/questions/15393363/how-to-disable-gzipcontent-in-cloud-endpoints-builder-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!