2016-01-21 72 views
0

我绝对是Android的初学者。现在我开始学习抽象HTTP请求和响应。我将响应数据绑定到Listview。我正在做这样的http获取请求。如何在Android中处理JsonObjectRequest数组和对象集合的响应json Volley

这是我与凌空要求

public class VolleyActivity extends Activity{ 
    private int lastSawFirstListItem; 
    private int itemLoadedOn; 
    private ArrayAdapter adapter; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.volley_main); 

     Button getBtn = (Button)findViewById(R.id.btn_get_request); 
     getBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

      } 
     }); 

     ListView listView = (ListView)findViewById(R.id.volleyListView); 
     adapter = new CustomAdapter(this,new Entity[0]); 
     listView.setAdapter(adapter); 
     String url = "http://api.androidhive.info/feed/feed.json"; 

     JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject jsonObject) { 
       List<Entity> items = new ArrayList<Entity>(); 
       try{ 
        jsonObject = jsonObject.getJSONObject("feed"); 
        //Then how to handle my response collection here 
        //because this is the combination of array and object collection 
       } 
       catch (JSONException e) 
       { 
        Toast.makeText(getBaseContext(),"JSON exception",Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError volleyError) { 

      } 
     }); 

     Volley.newRequestQueue(getBaseContext()).add(jsonRequest); 
} 

    public Entity convertToEntity(JSONObject item) throws JSONException 
    { 
     Entity en = new Entity(); 
     en.setId(item.getInt("id")); 
     en.setName(item.getString("name")); 
     en.setName(item.getString("url")); 
     return en; 
    } 
} 

这项活动类的JSON链接,我请求http://api.androidhive.info/feed/feed.json

所以请我怎么处理这个组合?我已经做出抛出错误的ArrayRequest。

我处理的响应回调这样

try{ 
        jsonObject = jsonObject.getJSONObject("feed"); 
        for(int i=0;i<jsonObject.length();i++){ 
         StringBuilder sb = new StringBuilder(); 
         sb.append(i); 
         String name = jsonObject.getJSONObject(sb.toString()).getString("name"); 
        } 
        Toast.makeText(getBaseContext(),"ok",Toast.LENGTH_SHORT).show(); 
       } 
       catch (JSONException e) 
       { 
        Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_SHORT).show(); 
       } 

但它在截图扔JSONException等。 enter image description here

+0

请发布错误。排队请求队列也可以做成一个单例实例。 您可以使用Gson基于POJO进行反序列化。 – TheSunny

+0

这个ObjectRequest不会抛出错误。我只是想知道如何遍历和处理。因为我从来没有这样做过。 –

+0

ArrayRequest将json结果数组作为错误消息抛出。 –

回答

1

我得到了答案。这就是我如何处理json响应,这是对我的问题的响应回调中的对象和数组的组合。

List<Entity> items = new ArrayList<Entity>(); 
       try{ 
        JSONArray jsonArray = jsonObject.getJSONArray("feed"); 
        for(int i=0;i<jsonArray.length();i++) 
        { 
         JSONObject item = jsonArray.getJSONObject(i); 
         //work with entity items 
        } 
        Toast.makeText(getBaseContext(),"Success",Toast.LENGTH_SHORT).show(); 
       } 
       catch (JSONException e) 
       { 
        Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_SHORT).show(); 
       } 
0

要提取的JSONObject的字符串使用 String s = jsonObject.getString("sJson"); // here sJosn is the key in jsonObject. 同样布尔使用Boolean b = jsonObject.getBoolean("key")和整数:int i = jsonObject.getInt("keyint");

+0

现在我喜欢这个。 jsonObject = jsonObject.getJSONObject(“feed”)。所以这将返回数组。那我该如何循环呢?然后每个迭代的项目都是对象。那么如何找回它的属性? –

+0

'this.jsonObject'是一个jsonObjects数组的对吗? 你可以简单地使用循环来迭代。像'for(int i = 0; i

+0

jsonException。这个截图只是显示了我使用try catch语句的jsonObject –