2016-07-29 2 views
1

목록보기에서 인쇄 된 json 응답이 있습니다. json의 값이 인쇄되지만 모든 값이 올바르게 인쇄되지 않습니다. 일부 json 값은 잘못된 위치에 인쇄됩니다.JSON 응답이 제대로 안드로이드에서 인쇄되지 않습니다.

JSON 응답 :

``` 
[ 
    { 
     "HouseNo":"33333333", 
     "AreaName":"ghfhgfhg", 
     "Landmark":"", 

    }, 
    { 

     "HouseNo":"33333333", 
     "AreaName":"gfhgfh", 
     "Landmark":"", 

    } 
] 

``

이 특정 값 페치 코드 :

StringRequest stringRequest = new StringRequest(Request.Method.POST,url, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         Log.d(TAG, response.toString()); 
         hidePDialog(); 




          try { 
           JSONArray jsonarray = new JSONArray(response); 
           for (int i = 0; i < jsonarray.length(); i++) { 
            JSONObject obj = jsonarray.getJSONObject(i); 
           Customer customer = new Customer(); 
           customer.setTitle(obj.getString("HouseNo")); 
           customer.setSerial(obj.getString("AreaName")); 
           customer.setService(obj.getString("Landmark")); 

           customerList.add(customer); 

          } 

         } catch (JSONException e) { 
           e.printStackTrace(); 
          } 

         // notifying list adapter about data changes 
         // so that it renders the list view with updated data 
         adapter.notifyDataSetChanged(); 
            } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
       hidePDialog(); 

      } 
     }) 

일부 확인 응답을 인쇄하는 단계; 그러나 일부 응답 대신 인쇄 된 다른 응답을 인쇄합니다.이 문제를 해결하는 방법?

+0

어댑터 코드 표시 – Vickyexpert

+0

또한 고객 모델 클래스 추가 – Pavya

답변

3

귀하의 JSON 구문 분석 코드가 어댑터 클래스를 ArrayAdapter와하는 확장하는 경우 어댑터 클래스에 추가() 메소드를 오버라이드 (override)하는 어 오른쪽입니다 이러한 변환에 대한 구글의 GSON 라이브러리를 사용할 수 있습니다.

@Override 
    public void add(Customer object) { 
     customerList.add(object); 
     super.add(object); 
    } 

이렇게하면 목록보기가 자동으로 업데이트됩니다.

BaseAdapter를 사용하는 경우 은 모델 클래스에 오류가 있거나 어댑터에 List 객체가 사용되었을 수 있습니다.

1

당신은

Customer c = new Gson().fromJson(json,new TypeToken<Customer>(){}.getType()); 
관련 문제