如何使用Volley

时间:2017-05-01 09:27:31

标签: android json

我的json数据格式如下:

[
  [
    {
      "jsonObject":"jsonObject",
      "jsonObject":"jsonObject",
      "jsonObject":"jsonObject"
    },
    {
      "jsonObject1":"jsonObject1",
      "jsonObject1":"jsonObject1",
      "jsonObject1":"jsonObject1"
    }
  ],
  [
    {
      "jsonObject2":"jsonObject2"
    },
    {
      "jsonObject2":"jsonObject2"
    }
  ]
]

我如何获得这个json格式并使用volley设置到listview我试过下面的代码,但我没有得到任何对象:

{
    JsonArrayRequest request = new JsonArrayRequest(Request.Method.POST, SUBSCRIPTION_Url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    if (response.equals("success")) {
                    } else {
                        try {
                            for (int i = 0; i < response.length(); i++) {

                                JSONArray ja = (JSONArray) response.get(i);
                                JSONObject jb = (JSONObject) ja.get(i);

                                title_array_sub.add(jb.getString("payment_id").toString());
                                notice_array_sub.add(jb.getString("period").toString());
                                title1_array_sub.add(jb.getString("renewal_date").toString());
                                notice1_array_sub.add(jb.getString("auto_renewal").toString());
                                invoice_array_sub.add(jb.getString("invoice").toString());
                                adapter1 = new CustomBaseAdapter(Transaction.this, title_array_sub, notice_array_sub,
                                        title1_array_sub, notice1_array_sub, invoice_array_sub);
                                list2.setAdapter(adapter1);
                            }

                        } catch (JSONException e1) {
                        } catch (ParseException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(KEY_MOVIE_ID, user_id);
                map.put(KEY_USER_ID, User_type);
                Log.d("movie_on_description", String.valueOf(map));
                return map;
            }
    };
    RequestQueue requestqueue = Volley.newRequestQueue(Transaction.this);
    requestqueue.add(request);
}

请帮助我也尝试不使用循环并获得0索引的单个字符串中的值,我得到的值,但在使用循环时没有得到值。 三江源

3 个答案:

答案 0 :(得分:1)

在onSuccess中尝试这个

    if (response.equals("success")) {
            } else {
                try {
                    for (int i = 0; i < response.length(); i++) {

                        JSONArray ja = response.getJsonArray(i);
                        for(int j=0;j<ja.length();j++){  
                            JSONObject jb = ja.getJsonObject(j);

                            title_array_sub.add(jb.getString("payment_id").toString());
                            notice_array_sub.add(jb.getString("period").toString());
                            title1_array_sub.add(jb.getString("renewal_date").toString());
                            notice1_array_sub.add(jb.getString("auto_renewal").toString());
                            invoice_array_sub.add(jb.getString("invoice").toString());
                        }

                    }
    adapter1 = new CustomBaseAdapter(Transaction.this, title_array_sub, notice_array_sub, title1_array_sub, notice1_array_sub, invoice_array_sub);
                        list2.setAdapter(adapter1);
                } catch (JSONException e1) {

                } catch (ParseException e1) {
                    e1.printStackTrace();
                }
            }

并且您还可以使用this链接进行json解析

答案 1 :(得分:0)

您继续在for循环中制作新的适配器并将其设置为列表视图。移动创建和分配for循环。

for (int i = 0; i < response.length(); i++) {   
    JSONArray ja= (JSONArray) response.get(i);
    JSONObject jb = (JSONObject) ja.get(i);

    title_array_sub.add(jb.getString("payment_id").toString());
    notice_array_sub.add(jb.getString("period").toString());
    title1_array_sub.add(jb.getString("renewal_date").toString());
    notice1_array_sub.add(jb.getString("auto_renewal").toString());
    invoice_array_sub.add(jb.getString("invoice").toString());      
}
adapter1 = new CustomBaseAdapter(Transaction.this, title_array_sub, notice_array_sub, title1_array_sub, notice1_array_sub,invoice_array_sub);
list2.setAdapter(adapter1);

您的回复包含更多JSONArrays,以便检索这些用途response.getJSONArray(Integer.valueOf(i))

您的JSONArraysJSONObjects组成,您可以使用

再次循环浏览这些对象
for (int i = 0; i < array.length(); i++) {
    JSONObject obj = array.getJSONObject(i);
    // do something
}

点击此处查看更多示例:JSON Array iteration in Android/Java

答案 2 :(得分:0)

它将被两个数组分类1)数组[0]和数组[1]。所以为了获得这些json数据,我就这样做了:

         StringRequest strRequest = new StringRequest(Request.Method.POST, SUBSCRIPTION_Url,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {

                                Log.d("responsetrans_sub",response);

                                if (response.trim().equals("success")) {

                                } else {

                                    try {
                                        JSONArray jsonObj = new JSONArray(response);

                                        JSONArray contacts =  jsonObj.getJSONArray(0);
                                        JSONArray contacts1 =  jsonObj.getJSONArray(1);
                                        for (int i = 0; i < contacts.length(); i++) {
                                            JSONObject contacting = contacts.getJSONObject(i);
                                            title_array_sub.add(contacting.getString("payment_id").toString());
                                            notice_array_sub.add(contacting.getString("period").toString());
                                            notice1_array_sub.add(contacting.getString("auto_renewal").toString());
                                            ivoice_array_sub.add(contacting.getString("invoice").toString());

                                        }
                                        for (int i = 0; i < contacts1.length(); i++) {

                                            JSONObject contacting1 = contacts1.getJSONObject(i);
                                            title1_array_sub.add(contacting1.getString("renewal_date").toString());


                                            adapter1 = new CustomBaseAdapter(Transaction.this, title_array_sub, notice_array_sub,title1_array_sub, notice1_array_sub,ivoice_array_sub);
                                            list_subscription.setAdapter(adapter1);

                                        }

                                    } catch (JSONException e1) {

                                    } catch (ParseException e1) {
                                        e1.printStackTrace();
                                    }

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

                            }
                        }) {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> map = new HashMap<String, String>();
                        map.put(KEY_USER_ID, user_id);
                        map.put(KEY_USER_TYPE, User_type);

                        return map;
                    }
                };
                RequestQueue requestqueue = Volley.newRequestQueue(Transaction.this);
                requestqueue.add(strRequest);**