1

我尝试使用 Twitter Get users/lookup 来查找用户信息。但是我在解析响应 json 文件时遇到了一些错误。请求 URI 为:“https://api.twitter.com/1/users/lookup.json?screen_name=nba”。我的代码是:

public String getInternetData() throws Exception{
    String data = null;
    try {
        URI website = new URI("https://api.twitter.com/1/users/lookup.json?screen_name=nba");
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(website);
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
        String str = EntityUtils.toString(entity);
        try {
            JSONObject jouser = new JSONObject(str);
            data = jouser.getString("followers_count");
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }catch(Exception e){
        Log.e("log_tag", "Error in http connection: " +e.toString());
    }
    return data;
}

响应 JSON 内容是

[
   {
      "notifications":false,
      "id":19923144,
      "profile_link_color":"177BC7",
      "profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1787324427\/National-Basketball-Association_normal.jpg",
      "profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/559347559\/12twitter_playoffs_0523.jpg",
      "id_str":"19923144",
      "following":false,
      "profile_use_background_image":true,
      "profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/1787324427\/National-Basketball-Association_normal.jpg",
      "utc_offset":-18000,
      "friends_count":988,
      "profile_text_color":"333333",
      "time_zone":"Eastern Time (US & Canada)",
      "default_profile":false,
      "followers_count":5095414,
      "name":"NBA",
      "profile_banner_url":"https:\/\/si0.twimg.com\/brand_banners\/NBA\/1335482314\/live",
      "url":"http:\/\/www.nba.com",
      "profile_sidebar_border_color":"eeeeee",
      "created_at":"Mon Feb 02 19:04:42 +0000 2009",
      "protected":false,
      "listed_count":28499,
      "profile_background_tile":false,
      "contributors_enabled":true,
      "profile_sidebar_fill_color":"ffffff",
      "geo_enabled":false,
      "description":"News and notes directly from the NBA.",
      "location":"New York, NY",
      "is_translator":false,
      "show_all_inline_media":true,
      "statuses_count":28818,
      "follow_request_sent":false,
      "lang":"en",
      "profile_background_color":"000000",
      "default_profile_image":false,
      "verified":true,
      "favourites_count":15,
      "screen_name":"NBA",
      "profile_background_image_url":"http:\/\/a0.twimg.com\/profile_background_images\/559347559\/12twitter_playoffs_0523.jpg"
   }
]

然后我得到了错误: *Error parsing data: org.json.JSONException: Value [{"location"...... 有人有想法吗?

4

1 回答 1

3

如果响应以[它开头意味着它实际上是一个 JSON 数组,而不是一个对象。然后,您将不得不执行以下操作:

JSONArray array = new JSONArray(str);
JSONObject jsonObject = array.getJSONObject(0);
String followersCount = jsonObject.getString("followers_count");
于 2012-05-23T18:25:55.283 回答