Android:G +登录并获取详细信息

时间:2014-05-02 04:43:27

标签: android google-plus

我正在做一个模块,我需要通过google plus登录用户。 我可以获得用户的姓名和电子邮件,但是在他/她的圈子中获得了人员列表。 有没有人知道如何获得人物圈子中的人员名单。??

这是我的代码。有些冗长..

public class SignIn extends Activity implements ResultCallback<People.LoadPeopleResult>,ConnectionCallbacks, OnConnectionFailedListener ,OnClickListener
{
    //declaration of variables  

        static final String EMAIL_PATTERN = 
            "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signin_layout);


        //googleapiclient initialization..
         mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API, null)
            .addScope(Plus.SCOPE_PLUS_LOGIN)
            .addScope(Plus.SCOPE_PLUS_PROFILE)
            .build();
        initializecontrols();
        cancel.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });
        login.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) 
            {
                String em=email.getText().toString();
                String ps=pswd.getText().toString();
                if(em.length()==0||ps.length()==0)
                {
                    Toast.makeText(getApplicationContext(), "Enter all the above", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    if(validate(em))
                    {
                        Toast.makeText(getApplicationContext(), "Login Sucess", Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "Wrong email format", Toast.LENGTH_SHORT).show();
                    }

                }
            }
        });
        fb.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) 
            {
                Toast.makeText(getApplicationContext(), "yet to do..", Toast.LENGTH_SHORT).show();
            }
        });
        gp.setOnClickListener(this);
    }
    public boolean validate(final String hex) 
    {

        matcher = pattern.matcher(hex);
        return matcher.matches();

    }
    public void onConnectionFailed(ConnectionResult result) {
          if (!mIntentInProgress) {
            // Store the ConnectionResult so that we can use it later when the user clicks
            // 'sign-in'.
            mConnectionResult = result;

            if (mSignInClicked) {
              // The user has already clicked 'sign-in' so we attempt to resolve all
              // errors until the user is signed in, or they cancel.
              resolveSignInError();
            }
          }
        }
    @Override
    public void onClick(View v) 
    {
        if(v.getId()==R.id.gplogin && !mGoogleApiClient.isConnecting())
        {
             mSignInClicked = true;
            mGoogleApiClient.connect();
        }
    }
    protected void onStart() {
        super.onStart();
//      mGoogleApiClient.connect();
    }

    protected void onStop() {
        super.onStop();
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }
    @Override
    public void onConnected(Bundle arg0) {
        mSignInClicked = false;
        Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
        getProfileInformation();
        Plus.PeopleApi.loadVisible(mGoogleApiClient, null)
          .setResultCallback(this);
        mGoogleApiClient.disconnect();
    }
    @Override
    public void onConnectionSuspended(int arg0) {
        mGoogleApiClient.connect();
    }
    @Override
    protected void onActivityResult(int requestCode, int responseCode,
            Intent intent) {
        if (requestCode == RC_SIGN_IN) {
            if (responseCode != RESULT_OK) {
                mSignInClicked = false;
            }

            mIntentInProgress = false;

            if (!mGoogleApiClient.isConnecting()) {
                mGoogleApiClient.connect();
            }
        }
    }
    private void signInWithGplus() {
        if (!mGoogleApiClient.isConnecting()) {
            mSignInClicked = true;
            resolveSignInError();
        }
    }
    private void resolveSignInError() {
        if (mConnectionResult.hasResolution()) {
            try {
                mIntentInProgress = true;
                mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
            } catch (SendIntentException e) {
                mIntentInProgress = false;
                mGoogleApiClient.connect();
            }
        }

      }
    private void getProfileInformation() {
        try {
            if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) 
            {
                Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
                String personName = currentPerson.getDisplayName();
                String personPhotoUrl = currentPerson.getImage().getUrl();
                String personGooglePlusProfile = currentPerson.getUrl();
                String nickname = currentPerson.getNickname();
                String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
                Log.e("Output :", "Name: " + personName + ", plusProfile: "
                        + personGooglePlusProfile + ", email: " + email
                        + ", Image: " + personPhotoUrl);
                Toast.makeText(getApplicationContext(),"Name : "+personName+"\n Email :"+email, Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(),
                        "Person information is null", Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onResult(LoadPeopleResult peopleData) 
    {
        Log.e("function","onResult");
         if (peopleData.getStatus().getStatusCode() == CommonStatusCodes.SUCCESS) {
                PersonBuffer personBuffer = peopleData.getPersonBuffer();
                try {
                  int count = personBuffer.getCount();
                  for (int i = 0; i < count; i++) {
                    Log.d("Person", "Display name: " + personBuffer.get(i).getDisplayName());
                  }
                } finally {
                  personBuffer.close();
                }
              } else {
                Log.e("person", "Error requesting visible circles: " + peopleData.getStatus());
              }
    }

}

根据教程onResult将在

时调用回调方法
Plus.PeopleApi.loadVisible(mGoogleApiClient, null)
          .setResultCallback(this);

此特定代码在OnConnected方法

中执行

1 个答案:

答案 0 :(得分:0)

您在提出请求后立即断开连接。

Google Play服务使用具有异步回调的客户端服务架构。断开连接实际上会关闭服务连接,从而阻止响应被传递给您。这类似于打电话给你的好友,说“嘿,怎么回事”,然后立即挂断你的手机。

顺便说一下,这里还有其他一些非标准的东西。一般来说,你应该在onStart()中调用connect(你已经注释掉了)。如果用户已经授予了您的应用程序权限,那么您可以立即开始获取数据并使用它执行您想要的操作。如果用户未授予权限或存在其他问题(例如Kindle Fire,Google Play服务不可用),那么您将获得onConnectionFailed。然后,您可以保持该状态并在用户单击按钮时调用该分辨率。这将导致更快捷的UI,使您更容易匹配connect()和disconnect()调用。

通常应在onStop()中调用disconnect()。这样,在您的活动被拆除之前,您不会断开连接。