2013-03-13 24 views
7

Tôi muốn tắt GZipContent cho lớp Google Cloud Endpoints sao cho POST có thể hoạt động trên máy chủ phát triển cục bộ.Cách tắt trình tạo GZipContent trong Cloud Endpoints trong Android

Phiên bản mới nhất GPE tạo xây dựng thiết bị đầu cuối này:

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

và tài liệu Google khuyến cáo sử dụng nó như thế này:

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

Có ai biết làm thế nào để vô hiệu hóa GZipContent cho thiết bị đầu cuối?

Cảm ơn.

Trả lời

11

Bạn có thể sử dụng:

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

Thay TictactoeRequest với lớp thích hợp cho các ứng dụng của bạn.

+0

Cảm ơn Dan! Điều đó có vẻ vừa phải (tôi nhận thấy sự kiểm soát trong xxxxxRequestInitializer nhưng không chắc chắn làm thế nào để sử dụng nó). Tôi sẽ kiểm tra sau này khi tôi làm xong công việc ban ngày của mình và chấp nhận câu trả lời sau đó. – aez

1

Tôi không phải là 100% lý do câu trả lời của Dan không hiệu quả đối với tôi, nhưng điều này đã giải quyết vấn đề này cho tôi.

/* 
* 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; 
} 
0

Đối với những người đang gặp khó khăn với lỗi tôi thấy, đó là java.io.EOFException, nhưng chỉ trong máy chủ phát triển. Dưới đây là cách tôi có thể khắc phục điều này, sử dụng ví dụ được nêu trong câu hỏi của 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(); 

Điều này có thể đơn giản hơn các câu trả lời hữu ích khác.

Các vấn đề liên quan