无法获取 google+ 的用户个人资料信息
Not able to get the userprofile info of google+
我能够检索 google 的访问令牌 account.But 我无法获取用户配置文件 info.I 正在获取空指针 exception.Why 我无法理解。
下面我提供了两种获取访问令牌和获取用户配置文件信息的方法。
如果你能帮助我就太好了
MainActivity.java
private void tryAuthenticate() {
if (isFinishing()) {
return;
}
mToken = null;
setProgressBarIndeterminateVisibility(true);
AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
try {
mToken =
GoogleAuthUtil.getToken(MainActivity.this, mChosenAccountName, "oauth2:"
+ Scopes.PLUS_LOGIN + " " + "https://www.googleapis.com/auth/plus.login" + " "+" https://www.googleapis.com/auth/plus.profile.agerange.read"+" " +YouTubeScopes.YOUTUBE + " "
+ YouTubeScopes.YOUTUBE_UPLOAD);
} catch (GooglePlayServicesAvailabilityException playEx) {
GooglePlayServicesUtil.getErrorDialog(playEx.getConnectionStatusCode(),
MainActivity.this, REQUEST_GMS_ERROR_DIALOG).show();
} catch (UserRecoverableAuthException userAuthEx) {
// Start the user recoverable action using the intent
// returned by
// getIntent()
startActivityForResult(userAuthEx.getIntent(), REQUEST_AUTHENTICATE);
return false;
} catch (IOException transientEx) {
// TODO: backoff
Log.e(this.getClass().getSimpleName(), transientEx.getMessage());
} catch (GoogleAuthException authEx) {
Log.e(this.getClass().getSimpleName(), authEx.getMessage());
}
return true;
}
@Override
protected void onPostExecute(Boolean hideProgressBar) {
invalidateOptionsMenu();
if (hideProgressBar) {
setProgressBarIndeterminateVisibility(false);
}
if (mToken != null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
saveAccount();
}
});
}
loadData();
}
};
task.execute((Void) null);
}
private void loadProfile() {
new AsyncTask<Void, Void, Person>() {
@Override
protected Person doInBackground(Void... voids) {
GoogleCredential credential = new GoogleCredential();
credential.setAccessToken(mToken);
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
Plus plus =
new Plus.Builder(httpTransport, jsonFactory, credential).setApplicationName(
Constants.APP_NAME).build();
try {
return plus.people().get("me").execute(); //here am getting null pointer exception
} catch (final GoogleJsonResponseException e) {
if (401 == e.getDetails().getCode()) {
Log.e(this.getClass().getSimpleName(), e.getMessage());
GoogleAuthUtil.invalidateToken(MainActivity.this, mToken);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
tryAuthenticate();
}
}, mCurrentBackoff * 1000);
mCurrentBackoff *= 2;
if (mCurrentBackoff == 0) {
mCurrentBackoff = 1;
}
}
} catch (final IOException e) {
Log.e(this.getClass().getSimpleName(), e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Person me) {
mUploadsListFragment.setProfileInfo(me);
}
}.execute((Void) null);
}
你好像把很多东西混在一起了。您获得空指针的行是因为 me
未在程序中的其他任何地方定义,因此当调用 get 方法时它无法找到该值。
其次,如果你想执行一组两个不同的操作,你可以将代码分开在两个不同的文件中。如果您只想检索有关 Google+ 用户帐户的个人资料信息,请按照 tutorial.
我能够检索 google 的访问令牌 account.But 我无法获取用户配置文件 info.I 正在获取空指针 exception.Why 我无法理解。 下面我提供了两种获取访问令牌和获取用户配置文件信息的方法。
如果你能帮助我就太好了
MainActivity.java
private void tryAuthenticate() {
if (isFinishing()) {
return;
}
mToken = null;
setProgressBarIndeterminateVisibility(true);
AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
try {
mToken =
GoogleAuthUtil.getToken(MainActivity.this, mChosenAccountName, "oauth2:"
+ Scopes.PLUS_LOGIN + " " + "https://www.googleapis.com/auth/plus.login" + " "+" https://www.googleapis.com/auth/plus.profile.agerange.read"+" " +YouTubeScopes.YOUTUBE + " "
+ YouTubeScopes.YOUTUBE_UPLOAD);
} catch (GooglePlayServicesAvailabilityException playEx) {
GooglePlayServicesUtil.getErrorDialog(playEx.getConnectionStatusCode(),
MainActivity.this, REQUEST_GMS_ERROR_DIALOG).show();
} catch (UserRecoverableAuthException userAuthEx) {
// Start the user recoverable action using the intent
// returned by
// getIntent()
startActivityForResult(userAuthEx.getIntent(), REQUEST_AUTHENTICATE);
return false;
} catch (IOException transientEx) {
// TODO: backoff
Log.e(this.getClass().getSimpleName(), transientEx.getMessage());
} catch (GoogleAuthException authEx) {
Log.e(this.getClass().getSimpleName(), authEx.getMessage());
}
return true;
}
@Override
protected void onPostExecute(Boolean hideProgressBar) {
invalidateOptionsMenu();
if (hideProgressBar) {
setProgressBarIndeterminateVisibility(false);
}
if (mToken != null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
saveAccount();
}
});
}
loadData();
}
};
task.execute((Void) null);
}
private void loadProfile() {
new AsyncTask<Void, Void, Person>() {
@Override
protected Person doInBackground(Void... voids) {
GoogleCredential credential = new GoogleCredential();
credential.setAccessToken(mToken);
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
Plus plus =
new Plus.Builder(httpTransport, jsonFactory, credential).setApplicationName(
Constants.APP_NAME).build();
try {
return plus.people().get("me").execute(); //here am getting null pointer exception
} catch (final GoogleJsonResponseException e) {
if (401 == e.getDetails().getCode()) {
Log.e(this.getClass().getSimpleName(), e.getMessage());
GoogleAuthUtil.invalidateToken(MainActivity.this, mToken);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
tryAuthenticate();
}
}, mCurrentBackoff * 1000);
mCurrentBackoff *= 2;
if (mCurrentBackoff == 0) {
mCurrentBackoff = 1;
}
}
} catch (final IOException e) {
Log.e(this.getClass().getSimpleName(), e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Person me) {
mUploadsListFragment.setProfileInfo(me);
}
}.execute((Void) null);
}
你好像把很多东西混在一起了。您获得空指针的行是因为 me
未在程序中的其他任何地方定义,因此当调用 get 方法时它无法找到该值。
其次,如果你想执行一组两个不同的操作,你可以将代码分开在两个不同的文件中。如果您只想检索有关 Google+ 用户帐户的个人资料信息,请按照 tutorial.