我的源代码存在问题,清单文件中的权限可能正常,我使用生成的指纹(SHA1)在Google Developer Console中创建了ClientID。有谁知道为什么返回带有null结果的Plus.PeopleApi.getCurrentPerson(myClient)?
package com.getgender;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.plus.People;
import com.google.android.gms.plus.People.LoadPeopleResult;
import com.google.android.gms.plus.Plus;
import com.google.android.gms.plus.PlusClient;
import com.google.android.gms.plus.model.people.Person;
import android.app.Activity;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity
implements
com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks,
com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener, ConnectionCallbacks, ResultCallback<LoadPeopleResult> {
private static final int RC_SIGN_IN = 0;
private GoogleApiClient myClient;
private boolean mIntentInProgress;
private PlusClient mPlusClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myClient = new GoogleApiClient.Builder(this).addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_PROFILE).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
// myClient.connect();
//plusClient = new PlusClient.Builder(this, this, this).setScopes(Scopes.PROFILE, Scopes.PLUS_ME ,"https://www.googleapis.com/auth/userinfo.email").build();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Toast.makeText(getApplicationContext(), "Connection failed",
Toast.LENGTH_SHORT).show();
if (!mIntentInProgress && result.hasResolution()) {
try {
mIntentInProgress = true;
result.startResolutionForResult(this, RC_SIGN_IN);
} catch (SendIntentException e) {
// The intent was canceled before it was sent. Return to the
// default
// state and attempt to connect to get an updated
// ConnectionResult.
mIntentInProgress = false;
myClient.connect();
}
}
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
// Toast.makeText(getApplicationContext(), "Connected",
// Toast.LENGTH_SHORT).show();
String name = Plus.AccountApi.getAccountName(myClient);
Toast.makeText(getApplicationContext(), name,
Toast.LENGTH_SHORT).show();
Plus.PeopleApi.loadVisible(myClient, null).setResultCallback(this);
// After that fetch data
if (Plus.PeopleApi.getCurrentPerson(myClient) != null) {
Person currentPerson = Plus.PeopleApi
.getCurrentPerson(myClient);
String personName = currentPerson.getDisplayName();
String currentPerson2 = Plus.AccountApi.getAccountName(myClient);
Toast.makeText(getApplicationContext(), personName,
Toast.LENGTH_SHORT).show();
}
// int a = plusClient.getCurrentPerson().getGender();
// Toast.makeText(getApplicationContext(), a, Toast.LENGTH_SHORT).show();
// People g = Plus.PeopleApi.getCurrentPerson(myClient);
// Person p = Plus.PeopleApi.getCurrentPerson(myClient);
// String a = p.getBirthday();
//Plus.PeopleApi.
if (Plus.PeopleApi.getCurrentPerson(myClient) != null) {
Toast.makeText(getApplicationContext(), "nem null", Toast.LENGTH_SHORT).show();
// getData();
}else{
Toast.makeText(getApplicationContext(), "null", Toast.LENGTH_SHORT).show();
}
// int a = Plus.PeopleApi.getCurrentPerson(myClient).getGender();
// Toast.makeText(getApplicationContext(), a,
// Toast.LENGTH_SHORT).show();
// try {
// if (Plus.PeopleApi.getCurrentPerson(myClient) != null) {
// Person currentPerson = Plus.PeopleApi
// .getCurrentPerson(myClient);
// String personName = currentPerson.getDisplayName();
// // String personPhotoUrl = currentPerson.getImage().getUrl();
// // String personGooglePlusProfile = currentPerson.getUrl();
// // String email = Plus.AccountApi.getAccountName(myClient);
// int a = currentPerson.getGender();
//
// Toast.makeText(getApplicationContext(), personName,
// Toast.LENGTH_SHORT).show();
//
//
//
// } else {
// Toast.makeText(getApplicationContext(),
// "Person information is null", Toast.LENGTH_LONG).show();
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
}
// public void getData(){
// Person currentPerson = Plus.PeopleApi.getCurrentPerson(myClient);
// String curr = currentPerson.getDisplayName();
// Toast.makeText(getApplicationContext(), curr, Toast.LENGTH_SHORT).show();
// }
@Override
public void onConnectionSuspended(int arg0) {
Toast.makeText(getApplicationContext(), "Connection suspended",
Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
myClient.connect();
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(getApplicationContext(), "onStart", Toast.LENGTH_SHORT)
.show();
myClient.connect();
}
@Override
public void onDisconnected() {
// TODO Auto-generated method stub
}
@Override
public void onResult(LoadPeopleResult arg0) {
// TODO Auto-generated method stub
}
}