-4

I want to parse JSON Array and display result in listview. I have already ask in this community but didn't get helpful answer. Please give me code for this JSON.

JSON

[{
    "city_id": "1",
    "city_name": "Noida"
},
{
    "city_id": "2",
    "city_name": "Delhi"
},
{
    "city_id": "3",
    "city_name": "Gaziyabad"
},
{
    "city_id": "4",
    "city_name": "Gurgaon"
},
{
    "city_id": "5",
    "city_name": "Gr. Noida"
}]

URL

http://14.140.200.186/Hospital/newget_city.php

please help

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Shiv Rathore
  • 83
  • 1
  • 9
  • what is your listview looklike? – Linh Feb 03 '16 at 10:38
  • 6
    Possible duplicate of [Json Parsing in Android Application](http://stackoverflow.com/questions/3819273/json-parsing-in-android-application) – Ravindra Kushwaha Feb 03 '16 at 10:38
  • The question is really broad. You should address one issue at a time. Are you facing issues in Fetching the JSON, parsing the JSON or populating the ListView? Start with one, learn how to do it, and if you face issues, post your question here, with the code that doesn't work at your end. – Shamas S Feb 03 '16 at 10:41
  • @Phan Văn Linh nothing is going in listview – Shiv Rathore Feb 03 '16 at 10:48
  • @Shamas i want to simply show cities in listview from the url when i trying this nothig is going in listview i have already post the locat and code click on the link you can see:- http://stackoverflow.com/questions/35170183/logcat-error-in-android-studio?noredirect=1#comment58058833_35170183 – Shiv Rathore Feb 03 '16 at 10:50
  • @Satyamrathore: which library are you using for json parsing ? – thedarkpassenger Feb 03 '16 at 10:56
  • @Satyamrathore .... It was yours previous same question http://stackoverflow.com/q/35721219/3946958 with Tom user name.. Did you not get proper solution yet??Let me know where did u stuck?? – Ravindra Kushwaha Mar 02 '16 at 10:24
  • this question has been solved – Shiv Rathore Mar 02 '16 at 10:32
  • Link given by you is asking that when click on cityname then id of the city should be store in database table, table already has city_id and all the id of all cities should be store in city_id on clicking city. have you got it – Shiv Rathore Mar 02 '16 at 10:35

2 Answers2

0

As mentioned the question is too broad, just to give the approach I would take.

  1. Build the model class in this case: City
  2. I would advise using retrofit(http://square.github.io/retrofit/) for the network call, so build the interface
  3. Make the network call
  4. Add the retrieved results in a recyclerview adapter
gilokimu
  • 531
  • 4
  • 10
0

Using Gson can do the json parsing job for you. Its easy to integrate and handy to parse json data. You simply need to create a class containing city_id and city_name.

City.java

public class City {

    private String city_id;
    private String city_name;

    public City() {

    }

    public String getCityId() {
        return city_id;
    }

    public String getCityName() {
        return city_name;
    }
}

Now add another class. E.g. CityList.java

import java.util.List;

public class CityList {

    private List<City> cityList;

    public CityList() {
    }

    public List<City> getCityList() {
        return cityList;
    }

}

Now from json string, parse the data into the CityList class.

Gson gson = new Gson();
CityList myCityList = gson.fromJson(jsonString, CityList.class);

Add the gradle dependency for Gson in build.gradle

compile 'com.google.code.gson:gson:2.3'
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98