为 Android HTTP 请求创建 Google HTTP 传输对象

发布于 2024-11-11 02:27:25 字数 1353 浏览 3 评论 0原文

我正在创建一个 Android 应用程序并尝试通过 Google API 客户端使用 Google Places API。我一直在关注这个例子: http://ddewaele.blogspot.com/2011/05/introducing -google-places-api.html

我在创建用于创建 HTTP 请求工厂的 HTTP 传输对象时遇到问题。一切都在 Eclipse 中编译得很好,但是当我在手机上运行时进行调试时,我在第一行得到一个 classnotfound :

HttpTransport transport = new ApacheHttpTransport();
HttpRequestFactory httpRequestFactory = createRequestFactory(transport);
public static HttpRequestFactory createRequestFactory(HttpTransport transport) {
    return transport.createRequestFactory(new HttpRequestInitializer() {
        @Override
        public void initialize(HttpRequest request) throws IOException {
            request.headers.authorization = "...";
        }
    });
}

根据 HTTP 传输类的 JavaDoc:

安卓:

  • 从 SDK 2.3 开始,强烈建议使用 com.google.api.client.javanet.NetHttpTransport。他们的 Apache HTTP 客户端 实施维护得不太好。
  • 对于 SDK 2.2 及更早版本,请使用 com.google.api.client.apache.ApacheHttpTransport。 com.google.api.client.javanet.NetHttpTransport 由于存在一些错误,不推荐 Android SDK 实现 HttpURLConnection。

我在 2.2 上运行,所以我尝试使用 ApacheHTTPTransport。我也尝试过 NetHTTPTransport,但我得到了同样的结果(classnotfound)。

有什么想法/例子吗?

谢谢!

I'm creating an Android app and trying to use Google Places API through Google APIs Client. I've been following this example:
http://ddewaele.blogspot.com/2011/05/introducing-google-places-api.html

I'm having problems creating a HTTP Transport Object to use in creating the HTTP Request Factory. Everything compiles fine in eclipse, but when I debug while running on my phone, I get a classnotfound on the first line here:

HttpTransport transport = new ApacheHttpTransport();
HttpRequestFactory httpRequestFactory = createRequestFactory(transport);
public static HttpRequestFactory createRequestFactory(HttpTransport transport) {
    return transport.createRequestFactory(new HttpRequestInitializer() {
        @Override
        public void initialize(HttpRequest request) throws IOException {
            request.headers.authorization = "...";
        }
    });
}

According to the JavaDoc for the HTTP Transport Class:

Android:

  • Starting with SDK 2.3, strongly recommended to use
    com.google.api.client.javanet.NetHttpTransport. Their Apache HTTP Client
    implementation is not as well maintained.
  • For SDK 2.2 and earlier, use com.google.api.client.apache.ApacheHttpTransport.
    com.google.api.client.javanet.NetHttpTransport
    is not recommended due to some bugs in
    the Android SDK implementation of
    HttpURLConnection.

I'm running on 2.2, so I tried using the ApacheHTTPTransport. I've also tried the NetHTTPTransport, but I get the same thing (classnotfound).

Any ideas/examples?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追星践月 2024-11-18 02:27:25

为了使用 Google API 客户端在 Android 环境中运行 Places API,您的 Android 项目中需要具有以下依赖项:

  • M2_REPO/commons-codec/commons-codec/1.3/commons-codec-1.3.jar
  • M2_REPO/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar
  • M2_REPO/com/google/api/client/google-api-client/1.4.1-beta/google-api-client-1.4.1-beta.jar
  • M2_REPO/com/google/api/client/google-api-client -googleapis/1.4.1-beta/google-api-client-googleapis-1.4.1-beta.jar
  • M2_REPO/com/google/code/gson/gson/1.6/gson-1.6.jar
  • M2_REPO/com/google/guava/guava/r08/guava-r08.jar
  • M2_REPO/org/apache/httpcomponents/httpclient/4.0.3/ httpclient-4.0.3.jar
  • M2_REPO/org/apache/httpcomponents/httpcore/4.0.1/httpcore-4.0.1.jar
  • M2_REPO/org/codehaus/jackson/jackson-core-asl/1.6.5/jackson-core-asl-1.6.5.jar
  • M2_REPO/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar

In order to run the Places API in an Android environment using the Google APIs Client, you'll need to have the following dependencies in your Android project :

  • M2_REPO/commons-codec/commons-codec/1.3/commons-codec-1.3.jar
  • M2_REPO/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar
  • M2_REPO/com/google/api/client/google-api-client/1.4.1-beta/google-api-client-1.4.1-beta.jar
  • M2_REPO/com/google/api/client/google-api-client-googleapis/1.4.1-beta/google-api-client-googleapis-1.4.1-beta.jar
  • M2_REPO/com/google/code/gson/gson/1.6/gson-1.6.jar
  • M2_REPO/com/google/guava/guava/r08/guava-r08.jar
  • M2_REPO/org/apache/httpcomponents/httpclient/4.0.3/httpclient-4.0.3.jar
  • M2_REPO/org/apache/httpcomponents/httpcore/4.0.1/httpcore-4.0.1.jar
  • M2_REPO/org/codehaus/jackson/jackson-core-asl/1.6.5/jackson-core-asl-1.6.5.jar
  • M2_REPO/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文