0
我嘗試使用Twitter獲取用戶/查找來查找用戶信息。但是在解析響應json文件時出現了一些錯誤。請求URI是:「https://api.twitter.com/1/users/lookup.json?screen_name=nba」。我的代碼是:在android中解析twitter json響應時出錯
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"
}
]
然後我得到了錯誤:*錯誤的數據進行解析:org.json.JSONException:值[{ 「位置」 ...... .. 有人有想法嗎?
它確實以[開始。但我找不到這個數組的名字。就像在這個文件中,我應該在這個表達式中替換「pepe」的參數:JSONArray array = new JSONArray(pepe)? –
pepe?什麼是佩佩?你在說什麼? – Cristian
你說得對。這是一個JSONArray,而不是JSONObject。 JSONArray array = new JSONArray(str);解決問題。謝謝身體! –