【问题标题】:android java: foreach through json array returning values?android java:foreach通过json数组返回值?
【发布时间】:2012-02-26 18:26:46
【问题描述】:

我无法在我的 java 代码中执行 for each 循环。我可以获得单个 json 结果,但是如何在这段代码中使用 for each 循环?

有人可以帮我吗?

public JSONObject feedTimeline(String username) throws ClientProtocolException, IOException, JSONException{
    StringBuilder url = new StringBuilder(URL);
    url.append(username);

    HttpGet get = new HttpGet(url.toString());
    HttpResponse response = client.execute(get);
    int status = response.getStatusLine().getStatusCode();
    if(status == 200){
        HttpEntity e = response.getEntity();
        String data = EntityUtils.toString(e);
        JSONArray timeline = new JSONArray(data);
        for (int i = 0; i < timeline.length(); i++) {
        JSONObject value= timeline.getJSONObject(i); //no error if this i is 0 and without for each loop
        return value; //getting errors because of this return tweets
        }


    }else{
        Toast.makeText(Feed.this,"error",Toast.LENGTH_SHORT);
        return null;
    }
}


public class Read extends AsyncTask<String, Integer, String>{

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        try {
            json = feedTimeline("name");
            return json.getString(params[0]); //this would need to change I assume?
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

我收到 JSONObject feedTimeline 的错误...如果我有 for 循环。但是如果我把那个 for 循环出来,而不是在 JSONObject value = timeline.getJSONObject(i) 中使用 i 并且没有像 01 这样的数字值,那么它会输出。

另外,我相信在类 Read 中,return json.getString(params[0]) 也需要在 for 循环中工作?我对 JAVA 很陌生,我正在尝试自己学习所有内容。

提前谢谢你!

【问题讨论】:

  • 字符串值 = timeline.getJSONString("你的字符串名称")
  • 嗨,对不起,我对 Java 还是有点陌生​​,你能解释一下你的意思吗?
  • 我正在获取 Twitter 信息,所以它只是 public static String URL = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=";
  • 发布你的错误我明天会看到....
  • public JSONObject feedTimeline(String username)... 行有一个错误,它只是说这个方法应该返回 JSONObject 类型的结果

标签: java android arrays json for-loop


【解决方案1】:
    public JSONObject feedTimeline(String username) throws ClientProtocolException, IOException, JSONException{
        StringBuilder url = new StringBuilder(URL);
        url.append(username);

        HttpGet get = new HttpGet(url.toString());
        HttpResponse response = client.execute(get);
        int status = response.getStatusLine().getStatusCode();
        if(status == 200){
            HttpEntity e = response.getEntity();
            String data = EntityUtils.toString(e);
            JSONArray timeline = new JSONArray(data);
            for (int i = 0; i < timeline.length(); i++) {
            JSONObject value= timeline.getJSONObject(i); //no error if this i is 0 and without for each loop
            return value; //getting errors because of this return tweets
            }


        }else{
            Toast.makeText(Feed.this,"error",Toast.LENGTH_SHORT);      
        }
// changed the position of return statement. This should work now.
          return null;
    }

【讨论】:

    猜你喜欢
    • 2023-04-07
    • 2021-11-27
    • 1970-01-01
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多