gpt4 book ai didi

未找到 Android 和 Google 客户端 API NetHttptransport 类

转载 作者:行者123 更新时间:2023-11-29 14:55:34 24 4
gpt4 key购买 nike

我第一次尝试为 Android 使用 Google 生成的 API 库。我从 Sample Program provided by Google 中获取了代码.我的代码看起来像

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

我在构建路径中添加了以下库:

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

和这些依赖库:

Guava -r09httpclient-4.0.3 jackson 核心-asl-1.6.7gson-1.6

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

我在 Windows 7 上使用 Eclipse Indigo。

我添加了以下 jar 文件:

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 核心-asl-1.6.7

这解决了我的问题。但是,我仍然不确定这些文件如何与 wiki 页面上的库列表相对应,也不确定为什么在编译期间解析我的导入时需要它们。

最佳答案

必须明确添加google-http-client-1.5.0-beta.jar的库引用(右键项目->属性->Java构建路径->添加外部JAR),不要相信类路径。如果不这样做,apk文件中不包含所需的数据。
您可以从这里下载 Google plus 库 google-plus-java-starter_v5.zip enter image description here

关于未找到 Android 和 Google 客户端 API NetHttptransport 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7648039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com