2012-02-26 1 views
1

내 Java 코드에서 각 루프에 대해 수행하는 데 문제가 있습니다. 단일 json 결과를 얻을 수 있지만이 코드에서 각 루프에 대해 어떻게 사용합니까?android java : 값을 반환하는 json 배열을 통한 foreach?

나를 도와 줄 사람이 있습니까?

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 ...에 오류가 발생합니다. 하지만 그 루프를 꺼내서 iJSONObject value = timeline.getJSONObject(i)에두고 0 또는 1과 같은 숫자 값을 사용하는 대신 출력합니다.

또한 클래스 읽기를 믿고 return json.getString(params[0])도 for 루프로 작업해야합니까? 나는 정말로 자바에 익숙하지 않고 모든 것을 혼자서 배우려고 노력하고 있습니다.

미리 감사드립니다.

+0

문자열 값 = timeline.getJSONString ("당신의 문자열 이름") –

+0

안녕, 나는, 나는 당신이 무엇을 의미하는지 나에게 설명 할 수있는, 여전히 자바에 작은 새 미안 해요? – 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; 
    } 
관련 문제