android java:foreach通过json数组返回值?
我在java代码中执行for-each循环时遇到问题。我可以得到单一的json结果,但是如何在代码中使用for-each循环 有人能帮我吗android java:foreach通过json数组返回值?,java,android,arrays,json,for-loop,Java,Android,Arrays,Json,For Loop,我在java代码中执行for-each循环时遇到问题。我可以得到单一的json结果,但是如何在代码中使用for-each循环 有人能帮我吗 public JSONObject feedTimeline(String username) throws ClientProtocolException, IOException, JSONException{ StringBuilder url = new StringBuilder(URL); url.append(username)
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(字符串用户名)抛出ClientProtocolException、IOException、JSONException{
StringBuilder url=新的StringBuilder(url);
url.append(用户名);
HttpGet=newhttpget(url.toString());
HttpResponse response=client.execute(get);
int status=response.getStatusLine().getStatusCode();
如果(状态==200){
HttpEntity e=response.getEntity();
字符串数据=EntityUtils.toString(e);
JSONArray时间线=新JSONArray(数据);
对于(int i=0;iJSONObject value=timeline.getJSONObject(I)
中使用I
,并使用一个类似0
或1
的数字值,那么它会输出
另外,我相信在类Read中,return json.getString(params[0])
也需要在as-for循环中工作?我对JAVA非常陌生,我正在尝试自己学习所有东西
提前感谢!公共JSONObject feedTimeline(字符串用户名)抛出ClientProtocolException、IOException、JSONException{
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;
}
StringBuilder url=新的StringBuilder(url);
url.append(用户名);
HttpGet=newhttpget(url.toString());
HttpResponse response=client.execute(get);
int status=response.getStatusLine().getStatusCode();
如果(状态==200){
HttpEntity e=response.getEntity();
字符串数据=EntityUtils.toString(e);
JSONArray时间线=新JSONArray(数据);
对于(int i=0;i
String value=timeline.getJSONString(“您的字符串名称”)嗨,对不起,我对Java还是有点陌生,您能解释一下您的意思吗?我正在获取twitter信息,所以它只是公共静态字符串URL=”http://api.twitter.com/1/statuses/user_timeline.json?screen_name="
发布您的错误,我明天会看到。公共JSONObject feedTimeline(字符串用户名)…上有一个错误。
行,它只是说这个方法应该返回JSONObject类型的结果