我想在我的应用上制作twitter feed话题标签。我只是按照这个网站上的教程:http://codehenge.net/blog/2011/05/android-programming-tutorial-a-simple-twitter-feed-reader/但我在“for(Object t:arr)”中出现错误,警告是“只能迭代一个数组......”如何解决这个问题?
public class Tweet {
public String username;
public String message;
public String image_url;
public Tweet(String username, String message, String url) {
this.username = username;
this.message = message;
this.image_url = url;
}
public ArrayList<Tweet> getTweets(String searchTerm, int page) {
String searchUrl =
"http://search.twitter.com/search.json?q=@"
+ searchTerm + "&rpp=100&page=" + page;
ArrayList tweets =
new ArrayList();
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(searchUrl);
ResponseHandler<String> responseHandler =
new BasicResponseHandler();
String responseBody = null;
try {
responseBody = client.execute(get, responseHandler);
} catch(Exception ex) {
ex.printStackTrace();
}
JSONObject jsonObject = null;
JSONParser parser=new JSONParser();
try {
Object obj = parser.parse(responseBody);
jsonObject=(JSONObject)obj;
}catch(Exception ex){
Log.v("TEST","Exception: " + ex.getMessage());
}
JSONArray arr = null;
try {
Object j = jsonObject.get("results");
arr = (JSONArray)j;
} catch(Exception ex){
Log.v("TEST","Exception: " + ex.getMessage());
}
for(Object t : arr) {
Tweet tweet = new Tweet(
((JSONObject)t).get("from_user").toString(),
((JSONObject)t).get("text").toString(),
((JSONObject)t).get("profile_image_url").toString()
);
tweets.add(tweet);
}
return tweets;
}}
答案 0 :(得分:0)
您正在尝试遍历JSONArray,但无法使用
For(object t: arr) {
对于JSONArrays。将该行替换为:
For (int i = 0; i < arr.length; i++) {