2016-12-14 3 views
0

저는 아래와 같은 JSON 구조를 가지고 있으며 절대로 이미지 노드에 연결할 수 없습니다. Volley 도서관에서 어떻게 할 수 있습니까? 어떤 아이디어라도 도움이 될 것입니다. 미리 감사드립니다.이미지 노드에 어떻게 접근 할 수 있습니까?

{ 
    "nodes": [ 
    { 
     "node": { 
     "id": "2536", 
     "title": "Die Düsseldorfer Rabbiner", 
     "image": { 
      "src": "how to reach here", 
      "alt": "", 
      "title": "© Landeshauptstadt Düsseldorf/Michael Gstettenbauer" 
     }, 
     "body": "some text", 
     "category": "Deutsch", 
     "created": "1481344134", 
     "url": "some text" 
     } 
    } 
    ] 
} 

코드 블록

JsonArrayRequest getRequest = new JsonArrayRequest(
      url, 
      new Response.Listener<JSONArray>() { 
       @Override 
       public void onResponse(JSONArray response) { 
        try { 
         JSONObject jsonObject = new JSONObject("response"); 
         JSONArray jsonArray = jsonObject.getJSONArray("nodes"); 
         for(int i = 0; i<jsonArray.length(); i++){ 
          JSONObject jo = jsonArray .getJSONObject(i); 
          String name = jo.getString("category"); 

          Toast.makeText(context, name, Toast.LENGTH_LONG).show(); 

         } 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 
+0

json을 구문 분석하는 방법은 수동으로 수행하거나 GSON과 같은 라이브러리를 사용하는 것입니다. –

+0

'새 JSONObject ("object")'어떻게 생각하니? – njzk2

+0

안녕하세요 Ravinder, 내 코드 블록을 추가했습니다. 확인해 주시겠습니까? 나는 제대로하고 있다고 생각하지만 효과가 없습니다. – nuhkoca

답변

1
From the look of things you should use JsonObjectRequest instead JsonArrayRequest. 

JsonObjectRequest request=new JsonObjectRequest(url, new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 

        try { 
         JSONArray array = response.getJSONArray("nodes"); 
         for(int i=0;i<array.length();i++){ 
          JSONObject object= array.getJSONObject(i); 
          JSONObject imageObject= object.getJSONObject("node").getJSONObject("image"); 
          Toast.makeText(TestActivity.this, imageObject.toString(), Toast.LENGTH_LONG).show(); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

       } 
      }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

       } 
      }); 
+0

안녕하세요, 저에게 도움이되었습니다. 감사! – nuhkoca

1

자바 모델은 JSON 정확히 일치해야합니다. JSON 스키마의 기본 요소는 개체이며 배열이 아닙니다. 그러므로 귀하의 요청은 JSONObjectRequest 아닌 JSONArrayRequest해야한다 : 당신이 볼 수있을 당신이 큰 데이터 세트가있는 경우

JSONObjectRequest request = new JSONObjectRequest(url, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONArray response) { 
        try { 
         JSONArray nodes = response.getJSONArray("nodes"); 
         for(int i = 0; i < nodes.length; i++) { 
          JSONObject obj = nodes.getJSONObject(i); 
          JSONObject node = obj.getJSONObject("node"); 
          JSONObject image = node.getJSONObject("image"); 
          String title = image.getString("title"); 

          Toast.makeText(context, title, Toast.LENGTH_LONG).show(); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 

, 이것은 지루한 얻을 수 있습니다. 그래서 JSON 구문 분석 라이브러리를 사용하는 것이 좋습니다. GSON, Moshi 또는 Jackson입니다. Volley에서 training documentation에있는이 라이브러리를 사용하기 위해 사용자 정의 요청을하는 방법에 대한 좋은 자습서가 있습니다.

+0

안녕하세요, 저에게 도움이되었습니다. 당신은 최고입니다. 감사! – nuhkoca

관련 문제