2012-02-26 2 views
1

私のJavaコードで各ループを実行するのに問題があります。私は単一のjson結果を得ることができますが、このコードではどのようにループごとにaを使用しますか?android java:foreachからjson配列までの値を返すか?

誰かが私を助けることができますか?

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

forループがある場合、JSONObject feedTimelineのエラーが発生します。しかし、私はそれをループのために取って、その代わりにをJSONObject value = timeline.getJSONObject(i)とし、0またはのような数値を持っていれば、出力します。

また、私はクラスの読み取りを信じて、return json.getString(params[0])もforループとして作業する必要がありますか?私はJAVAにはまったく新しく、私は自分ですべてを学ぼうとしています。

ありがとうございます!

+0

文字列値= timeline.getJSONString(「あなたの文字列の名前」) –

+0

こんにちは、私は申し訳ありませんが、私はまだJavaへ少し新しいよ、あなたは何を意味するか私に説明できますか? – hellomello

+0

私にあなたのURLを教えてもらえますか? –

答えて

0
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; 
    } 
関連する問題