无法获取google +的userprofile信息

时间:2015-07-01 07:36:39

标签: android youtube-api google-plus

我能够检索谷歌帐户的访问令牌。但我无法获取userprofile信息。我得到空指针异常。为什么我不明白。 下面我提供了两种方法,使用它们获取访问令牌并获取userprofile信息。

如果你帮助我会很棒。

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);
}

1 个答案:

答案 0 :(得分:0)

好像你把很多东西混合在一起很好。获得空指针的行是因为me未在程序中的任何其他位置定义,因此当调用get方法时,它无法找到该值。

其次,如果要执行一组两个不同的操作,可以将代码分成两个不同的文件。如果您只想检索有关Google+用户帐户个人资料信息的信息,请按照tutorial进行操作。