我正在尝试首次使用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
我在构建路径中添加了以下库:
谷歌-API的客户端 - 1.5.0-beta.jar 谷歌-API服务加v1-1.2.2-beta.jar
和这些依赖库:
番石榴-R09 HttpClient的-4.0.3 杰克逊核心ASL-1.6.7 GSON-1.6
我只是出于绝望而添加了依赖...因为我看不到我需要它们。我的所有import语句在编译时都能正确解析,为什么我会收到这个错误?
我在Windows 7上使用Eclipse Indigo。
我添加了以下jar文件:
谷歌-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
jackson-core-asl-1.6.7
答案 0 :(得分:6)
您必须明确添加google-http-client-1.5.0-beta.jar的库引用(右键单击项目 - >属性 - > Java构建路径 - >添加外部JAR),不要相信类路径。 如果你不这样做,在apk文件中不包含所需的数据 您可以从此处下载Google plus图书馆google-plus-java-starter_v5.zip
答案 1 :(得分:0)
答案 2 :(得分:0)
这个可能有点晚了,但你正在寻找的罐子是 谷歌-HTTP-客户1.5.0-beta.jar
此jar定义了com.google.api.client.http。*和com.google.api.client.json。*
答案 3 :(得分:0)
plus = Plus.builder(transport, jsonFactory).setApplicationName("Google-PlusSample/1.0")
.setHttpRequestInitializer(accessProtectedResource)
.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
@Override
public void initialize(JsonHttpRequest request) {
PlusRequest plusRequest = (PlusRequest) request;
plusRequest.setPrettyPrint(true);
}
}).build();
使用此代替
Plus plus = new Plus(new NetHttpTransport(), new JacksonFactory());