未找到 Android 和 Google 客户端 API NetHttptransport 类 [英] Android and Google client API NetHttptransport Class not found

查看:20
本文介绍了未找到 Android 和 Google 客户端 API NetHttptransport 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试将 Google 生成的 API 库用于 Android.我从 Google 提供的示例程序中获取了代码.我的代码看起来像

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());

    }
}

但是以下行:

private static final HttpTransport TRANSPORT = new NetHttpTransport();

引发异常:

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:

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

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

和这些依赖库:

番石榴-r09httpclient-4.0.3杰克逊核心asl-1.6.7gson-1.6

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

我只是出于绝望才添加了依赖项...因为我看不到我需要它们.我所有的导入语句都在编译时正确解析,为什么会出现这个错误?

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?

我在 Windows 7 上使用 Eclipse Indigo.

I'm using Eclipse Indigo on Windows 7.

我添加了以下 jar 文件:

I added the following jar files:

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
番石榴-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.

推荐答案

必须显式添加google-http-client-1.5.0-beta.jar的库引用(右键project->Properties->Java Build Path-> 添加外部 JAR),不要相信类路径.如果不这样做,在apk文件中不包含需要的数据.
您可以从这里下载 Google plus 库 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

这篇关于未找到 Android 和 Google 客户端 API NetHttptransport 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆