Android和谷歌客户端API NetHttptransport类未找到未找到、客户端、Android、NetHttptransport

2023-09-12 09:58:13 作者:ご盗贼耍↘蛋刀﹌

我想使用谷歌产生的API库首次为Android。我已经采取了code从该示例程序由谷歌提供。我的code看起来像

I am trying to use the Google generated API library for the first time for Android. I have taken code from the the Sample Program provided by Google. My code looks like

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import android.os.Bundle;

// Core Google API
import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessTokenRequest.GoogleAuthorizationCodeGrant;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAuthorizationRequestUrl;
import com.google.api.client.http.ByteArrayContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;

// Google+ API
import com.google.api.services.plus.*;

public class GooglePlusActivity extends Activity {

    // Want data about authenticated user
    private static final String SCOPE = "https://www.googleapis.com/auth/plus.me";
    //
    private static final String CALLBACK_URL = "urn:ietf:wg:oauth:2.0:oob";
    //
    private static final HttpTransport TRANSPORT = new NetHttpTransport();
    private static final JsonFactory JSON_FACTORY = new JacksonFactory();

    private static final String CLIENT_ID = "XXXXXXXXXXXXXXX";
    private static final String CLIENT_SECRET = "XXXXXXXXXXXXXX";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Generate the URL to which we will direct users
        String authorizeUrl = new GoogleAuthorizationRequestUrl(CLIENT_ID,
                CALLBACK_URL, SCOPE).build();

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        try {
            String authorizationCode = in.readLine();

            // Exchange for an access and refresh token
            GoogleAuthorizationCodeGrant authRequest = new GoogleAuthorizationCodeGrant(TRANSPORT,
                JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, authorizationCode, CALLBACK_URL);

            authRequest.useBasicAuthorization = false;

            AccessTokenResponse authResponse = authRequest.execute();

            String accessToken = authResponse.accessToken;

            GoogleAccessProtectedResource access = new GoogleAccessProtectedResource(accessToken,
                TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, authResponse.refreshToken);

            HttpRequestFactory rf = TRANSPORT.createRequestFactory(access);
            System.out.println("Access token: " + authResponse.accessToken);


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Plus plus = new Plus(new NetHttpTransport(), new JacksonFactory());

    }
}

但是下面一行:

However the following line:

private static final HttpTransport TRANSPORT = new NetHttpTransport();

会导致异常:

cause an exception:

10-04 13:33:28.954: ERROR/AndroidRuntime(5925): Caused by: java.lang.NoClassDefFoundError: com.google.api.client.http.javanet.NetHttpTransport

我已经加入到构建路径以下库:

I have the following libraries added to the build path:

谷歌API的客户端1.5.0-beta.jar 谷歌-API的服务加v1-1.2.2-beta.jar

google-api-client-1.5.0-beta.jar google-api-services-plus-v1-1.2.2-beta.jar

和这些依赖库:

番石榴-R09 HttpClient的-4.0.3 杰克逊核心ASL-1.6.7 GSON-1.6

guava-r09 httpclient-4.0.3 jackson-core-asl-1.6.7 gson-1.6

我添加的依赖才走出绝望......因为我不能看到,我需要他们。我所有的import语句在编译时正确解析,所以我为什么会得到这个错误?

I added the dependencies only out of desperation...since I cannot see that I need them. All my import statements are resolved correctly at compile time, so why would I get this error?

我使用的Eclipse靛蓝在Windows 7上。

I'm using Eclipse Indigo on Windows 7.

我添加了以下jar文件:

I added the following jar files:

谷歌API的客户端1.5.0-β 谷歌-API的客户扩展-1.5.0-β 谷歌-API客户端 - 扩展 - android2-1.5.0-β 谷歌-API的服务加v1-1.2.2-β 谷歌-HTTP客户端-1.5.0-β 谷歌-HTTP客户端,扩展-1.5.0-β 谷歌-HTTP客户端 - 扩展 - android2-1.5.0-β 谷歌-支持OAuth客户端1.5.0-β 谷歌-支持OAuth客户扩展-1.5.0-β GSON-1.6 番石榴-R09 HttpClient的-4.0.3 的HttpCore-4.0.1 杰克逊核心ASL-1.6.7

google-api-client-1.5.0-beta google-api-client-extensions-1.5.0-beta google-api-client-extensions-android2-1.5.0-beta google-api-services-plus-v1-1.2.2-beta google-http-client-1.5.0-beta google-http-client-extensions-1.5.0-beta google-http-client-extensions-android2-1.5.0-beta google-oauth-client-1.5.0-beta google-oauth-client-extensions-1.5.0-beta gson-1.6 guava-r09 httpclient-4.0.3 httpcore-4.0.1 jackson-core-asl-1.6.7

这解决了我的问题。但是我仍然不知道如何将这些文件对应的库列表中的wiki页面上也是为什么他们需要我的时候进口在编译期间得到解决。

this resolved my problem. However I am still unsure about how these files correspond to the libraries list on the wiki page and also why they're required when my imports are resolved during compile time.

推荐答案

您必须明确添加谷歌-HTTP客户端-1.5.0-beta.jar库引用(右击项目 - >属性 - > Java构建Path- >添加外部JAR),不信类路径。 如果你不这样做,在apk文件不包含所需的数据。 您可以下载谷歌,加上库从这里 google-plus-java-starter_v5.zip

You must explicit add library reference of google-http-client-1.5.0-beta.jar(right click project->Properties->Java Build Path->Add External JARs), don't believe class path. If you do not do this, in the apk file does not contain the required data。 You can download Google plus library from here google-plus-java-starter_v5.zip