我在從twitter搜索feed解析json時遇到了問題。例如搜索網址是:json twitter在Android搜索解析中的提要
https://search.twitter.com/search.json?q=android
我想在JSON數據 「結果」 的數組。我的代碼獲取JSON和解析:
StringBuilder tweetFeedBuilder = new StringBuilder();
HttpClient tweetClient = new DefaultHttpClient();
//pass search URL string to fetch
HttpGet tweetGet = new HttpGet(searchURL);
//execute request
HttpResponse tweetResponse = tweetClient.execute(tweetGet);
//check status, only proceed if ok
StatusLine searchStatus = tweetResponse.getStatusLine();
if (searchStatus.getStatusCode() == 200) {
//get the response
HttpEntity tweetEntity = tweetResponse.getEntity();
InputStream tweetContent = tweetEntity.getContent();
//process the results
InputStreamReader tweetInput = new InputStreamReader(tweetContent);
BufferedReader tweetReader = new BufferedReader(tweetInput);
while ((lineIn = tweetReader.readLine()) != null)
{
tweetFeedBuilder.append(lineIn);
}
try{
// A Simple JSONObject Creation
JSONObject json=new JSONObject(tweetFeedBuilder);
Log.i("Tweets","<jsonobject>\n"+json.toString()+"\n</jsonobject>");
String str1 = "result";
JSONArray jarray = json.getJSONArray(str1);
for(int i = 0; i < jarray.length(); i++){
JSONObject c = jarray.getJSONObject(i);
String id = c.getString(TWEET_ID);
String text = c.getString(TWEET_TEXT);
}
}
catch(JSONException jexp){
jexp.printStackTrace();
}
創建JSON對象後,JSONArray給人創造的錯誤並且去catch塊。其實我想從JSON數據中獲取「結果」數組。
但創建時出錯。我只想從JSON數據中獲取user_id和文本。 我在android平臺和eclipse sdk上工作。
在「str1」中嘗試「結果」而不是「結果」。 – eightx2