为 Android HTTP 请求创建 Google HTTP 传输对象
我正在创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了使用 Google API 客户端在 Android 环境中运行 Places API,您的 Android 项目中需要具有以下依赖项:
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 :