Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取用户';s的电子邮件地址以及Google+;登录Android_Android_Google Plus - Fatal编程技术网

获取用户';s的电子邮件地址以及Google+;登录Android

获取用户';s的电子邮件地址以及Google+;登录Android,android,google-plus,Android,Google Plus,我正在开发一款Android应用程序,并正在实施Google+Sign 我有它的认证罚款,但是,我想得到用户的名字,姓氏和电子邮件地址 我正在使用下面的方法尝试获取用户的姓名,但是,这只会获取显示名,而不是我通过Google+Signin为web分别获取的名字和姓氏 if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) { Person currentPerson = Plus.Peo

我正在开发一款Android应用程序,并正在实施Google+Sign

我有它的认证罚款,但是,我想得到用户的名字,姓氏和电子邮件地址

我正在使用下面的方法尝试获取用户的姓名,但是,这只会获取显示名,而不是我通过Google+Signin为web分别获取的名字和姓氏

if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null)
        {
            Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
            Log.d("Display Name", currentPerson.getDisplayName());
}
第二个问题,我无法从回复中获取电子邮件。如果我调试应用程序并查看响应,我可以看到我的电子邮件地址,但由于代码中的某些原因,它只是循环并错过我的电子邮件,因此返回null

下面是电子邮件检索的代码

final String account = Plus.AccountApi.getAccountName(mGoogleApiClient);
        AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>()
        {
            @Override
            protected String doInBackground(Void... params) {
                HttpURLConnection urlConnection = null;

                try
                {
                    URL url = new URL("https://www.googleapis.com/plus/v1/people/me");
                    String sAccessToken = GoogleAuthUtil.getToken(SignIn.this, account,
                            "oauth2:" + Scopes.PLUS_LOGIN + " https://www.googleapis.com/auth/plus.profile.emails.read");

                    urlConnection = (HttpURLConnection)url.openConnection();
                    urlConnection.setRequestProperty("Authorization", "Bearer " + sAccessToken);

                    String content = CharStreams.toString(new InputStreamReader(urlConnection.getInputStream(),
                            Charsets.UTF_8));

                    if (!TextUtils.isEmpty(content))
                    {
                        JSONArray emailArray = new JSONObject(content).getJSONArray("emails");

                        for (int i = 0; i < emailArray.length(); i++)
                        {
                            JSONObject obj = (JSONObject)emailArray.get(i);

                            if (obj.getString("type") == "account")
                            {
                                return obj.getString("value");
                            }
                        }
                    }
                }
                catch (UserRecoverableAuthException ex)
                {
                    startActivityForResult(ex.getIntent(), RC_SIGN_IN_GET_EMAIL);
                    Log.e("SignIn", ex.toString());
                }
                catch (Exception ex)
                {
                    Log.e("SignIn", ex.toString());
                }
                finally
                {
                    if (urlConnection != null)
                    {
                        urlConnection.disconnect();
                    }
                }
                return null;
            }

            @Override
            protected  void onPostExecute(String info)
            {
                registerUserFromGooglePlusSignIn(info);
            }
        };
        task.execute();
    }
final String account=Plus.AccountApi.getAccountName(mGoogleApiClient);
AsyncTask任务=新建AsyncTask()
{
@凌驾
受保护字符串doInBackground(无效…参数){
HttpURLConnection-urlConnection=null;
尝试
{
URL=新URL(“https://www.googleapis.com/plus/v1/people/me");
String sAccessToken=GoogleAuthUtil.getToken(sign.this,account,
“oauth2:+Scopes.PLUS_LOGIN+”https://www.googleapis.com/auth/plus.profile.emails.read");
urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setRequestProperty(“授权”、“承载人”+sAccessToken);
String content=CharStreams.toString(新的InputStreamReader(urlConnection.getInputStream(),
字符集(UTF_8));
如果(!TextUtils.isEmpty(content))
{
JSONArray emailArray=新的JSONObject(内容).getJSONArray(“电子邮件”);
对于(int i=0;i
在您需要的连接上

Person person = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);

String name = person.getDisplayName(); //full name
Plus.AccountApi.getAccountName(mGoogleApiClient) //email

显示名称是您所需的“已连接”中的名字和姓氏

Person person = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);

String name = person.getDisplayName(); //full name
Plus.AccountApi.getAccountName(mGoogleApiClient) //email
显示名称是onConnected中的名字和姓氏

Person person = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
String firstName = person.getName().getGivenName();
String lastName = person.getName().getFamilyName();
检查更多字段

在未连接的

Person person = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
String firstName = person.getName().getGivenName();
String lastName = person.getName().getFamilyName();
检查更多字段

谢谢,我希望将它们作为单独的变量,在我的web应用程序的Javascript库中,我可以将它们单独取出,如果我使用Android的显示名称,我需要拆分字符串,我知道技术上应该不会有问题,但是,如果它在javascript中可用,那么它也应该在android中。android api没有为名字和姓氏提供单独的值。我想在这种情况下,我唯一的问题就是电子邮件地址的检索。谢谢,我想把它们作为单独的变量,在我的web应用程序的Javascript库中,我已经能够将它们分开,如果我使用Android的显示名,我就需要拆分字符串,我知道技术上应该不会有问题,但是,如果它在javascript中可用,那么它也应该在android中。android api没有为名字和姓氏提供单独的值。我想在这种情况下,我唯一的问题就是检索电子邮件地址