简体   繁体   English

只能迭代一个数组或java.lang.Iterable的实例

[英]Can only iterate over an array or an instance of java.lang.Iterable

Hi I keep getting this error, I'm trying to get tweets from twitter via JSON and cant seem to get rid of this on the "arr". 嗨,我一直收到此错误,我试图通过JSON从Twitter获得推文,但似乎无法在“ arr”上消除此错误。 Would I have to do a different for loop? 我需要做一个不同的for循环吗? I did try this but it keeps returning the same error. 我确实尝试过此方法,但始终返回相同的错误。

 public ArrayList<Tweet> getTweets(String searchTerm, int page) {
      String searchUrl = 
            "http://search.twitter.com/search.json?q=@" 
            + searchTerm + "&rpp=100&page=" + page;

      ArrayList<Tweet> tweets = 
            new ArrayList<Tweet>();

      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<Object> 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;
    }

Please help!! 请帮忙!!

As the exception says, you can only iterate with the for each loop on instances of java.lang.Iterable . 如异常所示,您只能对java.lang.Iterable实例的for每个循环进行迭代。 JSONArray is not one of them. JSONArray不是其中之一。

Use the "classic" for loop : 使用“经典”循环:

for(int i = 0; i < arr.length(); i++) {
   JSONObject t = (JSONObject) arr.get(i);
   Tweet tweet = new Tweet(
     t.get("from_user").toString(),
     t.get("text").toString(),
     t.get("profile_image_url").toString()
   );
   tweets.add(tweet);
 }

JSONArray doesn't implement Iterable . JSONArray没有实现Iterable Use length and get to access the members within it (see here ) 使用lengthget在其内访问的成员(参见这里

int count = arr.length();
for (int i=0;i<count;++i) {
  string s = arr.getString(i);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 只能迭代java.lang.Iterable的数组或实例吗? - Can only iterate over an array or an instance of java.lang.Iterable? 只能在java.lang.Iterable中的数组或实例上进行迭代 - Can only iterate over an array or an instance of java.lang.Iterable in java Eclipse 编译错误显示不正确(只能迭代数组或 java.lang.Iterable 的实例) - Eclipse compile error shown incorrectly (Can only iterate over an array or an instance of java.lang.Iterable) 错误:只能遍历数组或java.lang.Iterable的实例 - Error: can only iterate over an array or an instance of java.lang.Iterable 使用FileUtils时只能迭代数组或java.lang.Iterable的实例 - Can only iterate over an array or an instance of java.lang.Iterable while using FileUtils 另一个“只能迭代数组或java.lang.Iterable的实例”问题 - Yet another “Can only iterate over an array or an instance of java.lang.Iterable” issue 规则编译错误:只能迭代java.lang.Iterable的数组或实例 - Rule Compilation error : Can only iterate over an array or an instance of java.lang.Iterable ForStatement-只能迭代数组或java.lang的实例。在csv文件创建中可迭代 - ForStatement - Can only iterate over an array or an instance of java.lang.Iterable in csv file creation 在reducer中的for循环上获得编译错误“只能迭代数组或java.lang.Iterable实例” - Getting compilation error “Can only iterate over an array or an instance of java.lang.Iterable” at for loop in reducer 我正在使用Collection只能迭代数组或java.lang.Iterable的实例 - i am using Collection Can only iterate over an array or an instance of java.lang.Iterable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM