2016-07-01 2 views
0

에 표시됩니다 내가 webservice.I에 문제가 recyclerView으로 된 JSONObject를 다운로드하여 구문 분석 및 디스플레이 있어요. Volley 라이브러리를 사용하고 있습니다. 내 코드는 다음과 같습니다 콘서트는 3이고 내 recyclerView 정확히 3 콘서트를 가지고있는 동안 responseJSONArray의은 ListView에

@Override 
    protected List<Concert> doInBackground(String... params) { 
     final List<Concert> concerts = new ArrayList<>(); 
     StringRequest request = new StringRequest(Request.Method.GET, "http://krakowskascenamuzyczna.pl/api/koncerty/future/", 
       new com.android.volley.Response.Listener<String>() { 

       @Override 
       public void onResponse(String response) { 
        try { 
         JSONObject responseJSONObject = new JSONObject(response); 
         String arrayString = responseJSONObject.getJSONArray("posts").toString(); 
         JSONArray responseJSONArray = new JSONArray(arrayString); 
         Concert tempConcert; 
         Log.e(TAG, "onResponse: " + responseJSONArray.length()); 
         for (int i = 0; i < responseJSONArray.length(); i++) { 
          tempConcert = new Concert(responseJSONArray.getJSONObject(i).getJSONArray("attachments").getJSONObject(0).getJSONObject("images").getJSONObject("full").getString("url"), 
            responseJSONArray.getJSONObject(i).getString("content"), 
            responseJSONArray.getJSONObject(i).getString("date"), 
            responseJSONArray.getJSONObject(i).getString("title"), 
            responseJSONArray.getJSONObject(i).getInt("id"), 
            responseJSONArray.getJSONObject(i).getString("type"), 
            responseJSONArray.getJSONObject(i).getJSONObject("custom_fields").getJSONArray("typ").getInt(0)); 

          concerts.add(tempConcert); 
          Log.e(TAG, "onResponse: size" + concerts.size()); 
         } 

길이는 10입니다. 왜 모든 다운로드 콘서트가 목록에 추가되지 않습니까?

+0

합니까 로그 : 내부 루프 인쇄 10 회 "onResponse 크기"?? – sJy

+0

10 가지 답변을 얻으려면 RecyclerView의 구현을 살펴야합니다. –

+0

onResponse : 크기 ". 3 번 내가 org.json.JSONException를 얻을 것을 발견 인쇄 : 인덱스 0 범위를 벗어 [0..0) – Bartos

답변

1

이 작동합니다

try { 
    JSONObject jsonObject = new JSONObject(response); 
    JSONArray postsArray = jsonObject.getJSONArray("posts"); 
    JSONObject postObject; 
    Concert tempConcert; 
    JSONArray attachmentArray; 
    JSONObject imageObject; 
    JSONObject fullObject; 
    JSONArray typArray; 
    JSONObject customFieldObject; 
    for (int i = 0; i < postsArray.length(); i++) { 
     postObject = postsArray.getJSONObject(i); 
     tempConcert = new Concert(); 
     tempConcert.setId(postObject.getInt("id")); 
     tempConcert.setTitle(postObject.getString("title")); 
     attachmentArray = postObject.getJSONArray("attachments"); 
     if (attachmentArray != null && attachmentArray.length() > 0) { 
      imageObject = ((JSONObject) attachmentArray.get(0)).getJSONObject("images"); 
      if (imageObject != null) { 
       fullObject = imageObject.getJSONObject("full"); 
       if (fullObject != null) { 
        tempConcert.setUrl(fullObject.getString("url")); 
       } 
      } 
     } 
     tempConcert.setContent(postObject.getString("content")); 
     tempConcert.setDate(postObject.getString("date")); 
     tempConcert.setType(postObject.getString("type")); 
     customFieldObject = postObject.getJSONObject("custom_fields"); 
     if (customFieldObject != null) { 
      typArray = customFieldObject.getJSONArray("typ"); 
      if (typArray != null && typArray.length() > 0) { 
       tempConcert.setTyp((String) typArray.get(0)); 
      } 
     } 
     concerts.add(tempConcert); 
    } 
    Log.e("tag", "onResponse: size" + concerts.size()); 
} catch (JSONException e1) { 
    e1.printStackTrace(); 
    Log.d("TAG", "Exception caught : " + e1); 

} 
+0

일반 정수가 linetempConcert.setTyp 있도록 ((문자열) typArray.get (0))이다, 나는 tempConcert.setTyp로 변경 ((int)를 typArray.get (0));하지만 난 java.lang.ClassCastException가 얻을 : java.lang.String의는 java.lang.Integer에 – Bartos

+0

약간의 수정으로 캐스팅하고 완벽하게 작동 될 수 없습니다 :) 감사합니다 많이! 당신은 내 일을 저장! :) – Bartos

1

나는

String arrayString = responseJSONObject.getJSONArray("posts").toString(); 

JSONArray responseJSONArray = new JSONArray(arrayString); 

당신은 직접 된 JSONObject에서 JSONArray를 추출 할 수있는 일을 요점을 파악하지 않았다. 당신이 명확하게 카운트가이 도움이 (10)

희망이다시피

내가 안드로이드 스튜디오 로그 캣

enter image description here

이있어이 코드

JSONObject jsonObject = new JSONObject(response); 
JSONArray postsArray = jsonObject.getJSONArray("posts"); 
JSONObject postObject; 
for (int i = 0; i < postsArray.length(); i++) { 
    postObject = postsArray.getJSONObject(i); 
    int id = postObject.getInt("id"); 
    String title = postObject.getString("title"); 
    Log.d("TAG", "#" + i); 
    Log.d("TAG", "ID : " + id + ", title : " + title); 
} 

을보십시오.

+0

감사합니다, 나는 그것을 당신의 방법을했지만 여전히 3 항목을 가지고 :./ – Bartos

+0

당신이 예외를 받고 – apersiankite

+0

org.json.JSONException을 : 인덱스 0 범위 [0에서. 0.0) – Bartos