0

我可以使用 oath2 从 Google API 响应中获取用户信息。但我不知道如何单独获得这些回复。

我从 Google Api 得到的响应: *

Access token: ya29.AHES6ZQ3QxKxnfAzpZasdfd23423NuxJs29gMa39MXV551yMmyM5IgA 
     {
     "id": "112361XXXXXXXXXXX",
     "name": "Ansuman XX",
     "given_name": "Ansuman",
     "family_name": "Singh",
     "link": "https://plus.google.com/XXXXXXXXXXXXXXXXX",
     "gender": "male",
     "birthday": "0000-03-18",
     "locale": "en"
    }
    Original Token: ya29.AHES6ZQ3QxKxnfAzpZu0lYHYu8sdfsdafdgMa39MXV551yMmyM5IgA New Token: ya29.AHES6ZQ3QxKxnfdsfsdaYHYu8TNuxJs29gMa39MXV551yMmyM5IgA 

但我只想将“id”和“name”单独保存在我的数据库表中。我怎样才能做到这一点?

通过使用下面的代码,我得到了上述响应/输出。

             public static void main(String[] args) throws IOException 
          {

            -------------------------
            -------------------------
            -------------------------        

            String accessToken = authResponse.accessToken;
            GoogleAccessProtectedResource access = new GoogleAccessProtectedResource(accessToken, TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, authResponse.refreshToken); 
            HttpRequestFactory rf = TRANSPORT.createRequestFactory(access);
            System.out.println("Access token: " + authResponse.accessToken); 
            String url = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token="   + authResponse.accessToken;
                final StringBuffer r = new StringBuffer(); 
                    final URL u = new URL(url);
                    final URLConnection uc = u.openConnection();
                    final int end = 1000;
                    InputStreamReader isr = null;
                    BufferedReader br = null; 
                        isr = new InputStreamReader(uc.getInputStream());
                        br = new BufferedReader(isr);
                        final int chk = 0;
                        while ((url = br.readLine()) != null)
                        {
                            if ((chk >= 0) && ((chk < end)))
                            {
                                r.append(url).append('\n');
                            }
                         } 
              System.out.print("");
              System.out.println();
 System.out.print(" "+   r ); //this is printing at once but i want them individually
              access.refreshToken();
 System.out.println("Original Token: " + accessToken + " New Token: " + access.getAccessToken());
          }
4

1 回答 1

0

好吧,您可以在整个生成的 JSON 对象上使用像这样的 JSON 库(围绕简单性设计;此处的示例),或者您可以使用自己的正则表达式代码。类似以下的内容可能适用于后者:

//.... other existing code
String pat = "\"(.*)\": \"(.*)\"";
Pattern pattern = Pattern.compile(pat);
Matcher matcher = null;
Map<String,String> map = new HashMap<String,String>();

while ((url = br.readLine()) != null) {
    if ((chk >= 0) && ((chk < end))) {  
        matcher = pattern.matcher(url);
        if(matcher.find()) {
            map.put(matcher.group(1), matcher.group(2));
        }
        //r.append(url).append('\n');
    }
} 
System.out.println(map.get("id"));
//... rest of code

库方法可能是矫枉过正,但可能会处理数据类型转换等。也将处理非字符串数据类型与我悲伤的正则表达式。

于 2012-11-09T04:56:56.153 回答