class AsyncExecute extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
try {
// Llamamos al servicio web para recuperar los datos
httpHandler handler = new httpHandler();
txt = handler.post("http://comupunt.esy.es/nombreb.php", "pedro");
HttpGet httpGet = new HttpGet("http://comupunt.esy.es/nombreb.php");
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = (HttpResponse)httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
BufferedHttpEntity buffer = new BufferedHttpEntity(entity);
InputStream iStream = buffer.getContent();
String aux = "";
BufferedReader r = new BufferedReader(new InputStreamReader(iStream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
aux += line;
}
// Parseamos la respuesta obtenida del servidor a un objeto JSON
JSONObject jsonObject = new JSONObject(aux);
JSONArray cities = jsonObject.getJSONArray("cities");
// Recorremos el array con los elementos cities
for(int i = 0; i < cities.length(); i++) {
JSONObject city = cities.getJSONObject(i);
// Creamos el objeto City
City c = new City(city.getString("name"),
city.getInt("nametwo"),city.getString("posicion") );
c.setData(city.getString("photo"));
listaPersonas.add(c.photo);
// Almacenamos el objeto en el array que hemos creado anteriormente
citiesAvaiable.add(c);
}
}
catch(Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
// Creamos el objeto CityAdapter y lo asignamos al ListView
CityAdapter cityAdapter = new CityAdapter(MainActivity.this, citiesAvaiable);
lvCities.setAdapter(cityAdapter);
super.onPostExecute(result);
}
