Android - 使用volley从jsonarray填充列表

时间:2017-05-05 09:44:42

标签: android arrays json listview android-volley

我的应用程序应该发送源和目标作为参数以获得最快的总线路径,然后将它们作为数组发送回去。 我想使用volley从json数组中创建一个列表。这是我的数组输出的一个例子。

{  
   "arrayrute":[  
      "Halte LIK",
      "Halte Kampoeng Semarang",
      "Halte Kaligawe 2",
      "Halte Pasar Kobong",
      "Halte Kota Lama"
   ]
}

我怎么能从中列出一个清单?通常,数据库中的json数组在变量之前会有它的行名,所以我只需要使用list.set_id(jsonobject.getString("id"))list.set_id(jsonobject.optString("id"))

这是我获取数组的java代码

private void getData() {
        //Creating a string request
        asal = spin_dari.getSelectedItem().toString().trim();
        tujuan = spin_ke.getSelectedItem().toString().trim();

        StringRequest stringRequest = new StringRequest(Request.Method.GET, DIRECTION_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        JSONObject j = null;
                        try {
                            //Parsing the fetched Json String to JSON Object
                            j = new JSONObject(response);

                            //Storing the Array of JSON String to our JSON Array
                            results = j.getJSONArray("arrayrute");

                            //Calling method getStudents to get the students from the JSON Array
                            getdireksi(results);
                            //dirlist.add(results .toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        adaptdir.notifyDataSetChanged();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                })
        {
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                params.put(KEY_ASAL,asal);
                params.put(KEY_TUJUAN,tujuan);
                return params;
            }
        };

        //Creating a request queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        //Adding request to the queue
        requestQueue.add(stringRequest);
    }

    private void getdireksi(JSONArray j) {
        //Traversing through all the items in the json array
        for (int i = 0; i < j.length(); i++) {
            try {
                //Getting json object
                JSONObject json = j.getJSONObject(i);

                //Adding the name of the student to array list
                dirlist.add(json.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

请尝试这种方式。以下是 DEMO

 String url = "{  \n" +
                "   \"arrayrute\":[  \n" +
                "      \"Halte LIK\",\n" +
                "      \"Halte Kampoeng Semarang\",\n" +
                "      \"Halte Kaligawe 2\",\n" +
                "      \"Halte Pasar Kobong\",\n" +
                "      \"Halte Kota Lama\"\n" +
                "   ]\n" +
                "}";


        try {
            JSONObject jobject= new JSONObject(url);
            JSONArray jarray=jobject.getJSONArray("arrayrute");
            for(int k=0;k<jarray.length();k++)
            {
                String getValue=(jarray.getString(k));
                System.out.println("Intellij_Amiyo"+getValue);
            }

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

最终

 private void getdireksi(JSONArray j)
 {
    //Traversing through all the items in the json array
    for(int k=0;k<j.length();k++)
        try {
            String getValue=(j.getString(k));
            System.out.println("Intellij_Amiyo"+getValue);
            dirlist.add(json);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

答案 1 :(得分:0)

你的getdireksi()方法错了。请使用以下代码代替您的代码。

private void getdireksi(JSONArray j) {
    //Traversing through all the items in the json array
    for (int i = 0; i < j.length(); i++) {
        try {
            //Getting json object
            String json = j.getString(i);

            //Adding the name of the student to array list
            dirlist.add(json);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
相关问题