0

ここで Android-Google ドキュメントを参照しています: http://developer.android.com/google/play-services/auth.html

オブジェクトを使用せずに getFirstName() を呼び出すコードのこの部分があります。どのクラスのオブジェクトを作成または実装/拡張するかを推測できません。

これがコードのその部分です。残りはすべてそのリンクにあります。

URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + token);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
int serverCode = con.getResponseCode();
//successful query
if (serverCode == 200) {
    InputStream is = con.getInputStream();
    String name = getFirstName(readResponse(is)); //THIS LINE!!
    mActivity.show("Hello " + name + "!");
    is.close();
    return;
//bad token, invalidate and get a new one
} else if (serverCode == 401) {
    GoogleAuthUtil.invalidateToken(mActivity, token);
    onError("Server auth error, please try again.", null);
    Log.e(TAG, "Server auth error: " + readResponse(con.getErrorStream()));
    return;
//unknown error, do something else
} else {
    Log.e("Server returned the following error code: " + serverCode, null);
    return;
}

編集: メソッド getFirstName() を使用したいのですが、「解決できません」というエラーが表示されます。

4

1 に答える 1

0

を見ると<android-sdk>/extras/google-play-services/samples/auth/AbstractGetNameTask.java、 の定義がわかりますgetFirstname

private String getFirstName(String jsonResponse) throws JSONException {
  JSONObject profile = new JSONObject(jsonResponse);
  return profile.getString(NAME_KEY);
}

NAME_KEYそのファイルの前に定義されています。

private static final String NAME_KEY = "given_name";

Android SDK ディレクトリにそのファイルが見つからない場合は、Android SDK Manager を開いて「Google Play サービス」をダウンロードしてください。

于 2013-08-25T21:43:15.377 に答える