title: Hitting URL to get data and display data in list view
body: <p>I want to learn about JSON parsing. I want to fetch the data from a URL and display the data in a ListView, but I am getting an error:</p>
  
  <blockquote>
    <p>unfortunately app has stopped</p>
  </blockquote>
  
  <p>How can I solve this error? Any mistakes in my code?</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      new JSONTask().execute("14.140.200.186/Hospital/get_city.php");
  
  
  
  }
  public class JSONTask extends AsyncTask&lt;String,String, List&lt;CityModel&gt;&gt;{
      @Override
      protected void onPostExecute( List&lt;CityModel&gt; result) {
          super.onPostExecute(result);
          CityAdapter adapter=new CityAdapter(getApplicationContext(),R.layout.row,result);
          lvCity = (ListView) findViewById(R.id.lvCity);
          lvCity.setAdapter(adapter);
      }
      public class CityAdapter extends ArrayAdapter{
          private List&lt;CityModel&gt; cityModelList;
          private int resource;
          private LayoutInflater inflater;
          public CityAdapter(Context context, int resource, List&lt;CityModel&gt; objects) {
              super(context, resource, objects);
              cityModelList=objects;
              this.resource=resource;
              inflater= (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
          }
  
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
  
              if (convertView==null)
                  convertView=inflater.inflate(resource,null);
              TextView cityname;
              cityname= (TextView)convertView.findViewById(R.id.cityname);
              cityname.setText(cityModelList.get(position).getCity_name());
  
              return  convertView;
  
          }
      }
  
      @Override
      protected  List&lt;CityModel&gt; doInBackground(String... params) {
          HttpURLConnection connection = null;
          BufferedReader reader = null;
          try {
              URL url = new URL(params[0]);
              connection = (HttpURLConnection) url.openConnection();
              connection.connect();
              InputStream stream = connection.getInputStream();
              reader = new BufferedReader(new InputStreamReader(stream));
              StringBuffer buffer = new StringBuffer();
              String line = "";
              while ((line = reader.readLine()) != null) {
                  buffer.append(line);
              }
              String finalJson=buffer.toString();
              JSONObject parentObject=new JSONObject(finalJson);
              JSONArray parentArray=parentObject.getJSONArray("Cities");
              List&lt;CityModel&gt;cityModelList=new ArrayList&lt;&gt;();
              for (int i=0; i&lt;parentArray.length(); i++){
                  JSONObject finalObject = parentArray.getJSONObject(i);
                  CityModel cityModel=new CityModel();
                  cityModel.setCity_name(finalObject.getString("city_name"));
                  cityModelList.add(cityModel);
              }
              return cityModelList;
          } catch (MalformedURLException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } catch (JSONException e) {
              e.printStackTrace();
          } finally {
              if (connection != null)
                  connection.disconnect();
              ;
              try {
                  if (reader != null)
                      reader.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
  
          }
  
  
          return null;
      }
  }
  </code></pre>