Creating a Google HTTP Transport Object for Android HTTP Request

余生长醉 提交于 2020-01-02 08:56:32

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/6210139/creating-a-google-http-transport-object-for-android-http-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!