7

我想为 Google Cloud Endpoints 类禁用 GZipContent,以便 POST 可以在本地开发服务器上运行。

最新的 GPE 版本生成此端点构建器:

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

谷歌文档建议像这样使用它:

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

有谁知道如何为端点禁用 GZipContent ?

谢谢。

4

3 回答 3

11

您可以使用:

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

替换TictactoeRequest为适合您的应用程序的类。

于 2013-03-13T18:33:17.000 回答
1

我不是 100% 为什么 Dan 的回答对我不起作用,但是这个GitHub 项目的代码为我解决了这个问题。

/*
 * 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;
}
于 2014-05-31T10:32:03.700 回答
0

对于那些在谷歌上搜索我看到的错误的人,它是 java.io.EOFException,但仅在开发服务器中。使用 OP 问题中所述的示例,这是我能够解决此问题的方法:

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

这可能比其他有用的答案要简单一些。

于 2014-12-19T00:12:22.867 回答