这与另一个关于 URL 缩短器的问题有关。URL 缩短器的代码在独立 Java 和 Android 上都可以正常工作。但是,当重新使用此代码与自定义 Google App Engine 应用程序通信时,该代码在独立 Java 上运行良好,但在 Android 上使用时会产生一个空的 self.request.body。为什么?
要重现此行为,请在 android 上运行以下代码,一次使用 Google 缩短 URL:
https://www.googleapis.com/urlshortener/v1/url
,一次使用指向 Web 服务器的自定义 URL。在两种情况下,独立 java 上的相同代码会产生完全相同的请求(如预期的那样)。在 Android 上,在自定义 URL 的情况下,内容正文为空且未设置 content-length。我正在使用杰克逊 1.7.1 运行 google-api-java-client 1.2.2-alpha。
代码片段:
HttpTransport transport = GoogleTransport.create();
HttpRequest request = transport.buildPostRequest();
// Change this URL below from Google Shortener URL, to a custom URL,
// and the code on Android produces an empty body and Content-Length is not set
request.setUrl("GoogleAppEngine url goes here");
JsonCContent content = new JsonCContent();
GenericData data = new GenericData();
data.put("id", "whatever");
content.data = data;
request.content = content;
HttpResponse response = request.execute();