wibisono indiarto
wibisono indiarto

Reputation: 37

Android - populating list from jsonarray using volley

My app supposedly to send a source and a destination as parameters to get the fastest bus route and then send them back as an array. I want to make a list from a json array using volley. Here is an example of my array output.

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

How am I supposedly to make a list from it? Usually a json array from a database would have it's row name before the variable so I just have to use list.set_id(jsonobject.getString("id")) or list.set_id(jsonobject.optString("id"))

Here's my java code to get the array

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();
            }
        }
    }

Upvotes: 0

Views: 374

Answers (2)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

Please Try with this way. Below is 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();
        }

Final

 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();
        }
    }
}

Upvotes: 1

R.R.M
R.R.M

Reputation: 790

Your getdireksi() method is wrong. Put following code instead of yours.

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();
        }
    }
}

Upvotes: 0

Related Questions