Contenido sobre Android
Buscar
Social
Ofertas laborales ES

Foro sobre Android > Se ejecuta antes una parte del código que otra. No le veo sentido

Hola, estoy teniendo un problema debido a que tengo el siguiente código y no entiendo por qué se ejecuta antes unos System.out.println que otros(os pongo lo que sale en Android Monitor). Pongo en negrita lo importante para daros mayor legibilidad.


private double latitud, longitud;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mostrarinfoanuncio);

//las conecto al xml
imagenAnuncio = (ImageView) findViewById(R.id.imageViewAnuncio);
textoAnuncioLocalidad = (TextView) findViewById(R.id.textViewLocalidad);
textoAnuncioNombre = (TextView) findViewById(R.id.textViewNombre);
textoAnuncioHoraLunes = (TextView) findViewById(R.id.textViewHoraLunes);
textoAnuncioHoraMartes = (TextView) findViewById(R.id.textViewHoraMartes);
textoAnuncioHoraMiercoles = (TextView) findViewById(R.id.textViewHoraMiercoles);
textoAnuncioHoraJueves = (TextView) findViewById(R.id.textViewHoraJueves);
textoAnuncioHoraViernes = (TextView) findViewById(R.id.textViewHoraViernes);
textoAnuncioHoraSabado = (TextView) findViewById(R.id.textViewHoraSabado);
textoAnuncioHoraDomingo = (TextView) findViewById(R.id.textViewHoraDomingo);

textoAnuncioCondiciones = (TextView) findViewById(R.id.textViewCondiciones);
textoUbicacion = (TextView) findViewById(R.id.textView11);

mapView = (MapView) findViewById(R.id.mi_mapa);


//para recoger el id que me mandan el el intent desde MainActivity
Intent intent = getIntent();
Bundle bundle = intent.getExtras();

if (bundle != null) {
String idAnuncio = (String) bundle.get("id");
final String servidor = (String) bundle.get("ipservidor");


String ficherophp = "obtenerDatosUnAnuncio.php";
final ProgressDialog progressDialog = new ProgressDialog(MostrarinfoanuncioActivity.this);
progressDialog.setMessage("Cargando datos...");
progressDialog.show();
AsyncHttpClient cliente = new AsyncHttpClient();
RequestParams parametro = new RequestParams();
parametro.put("parametro", idAnuncio);
System.out.println("aqui1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
System.out.println("ANUUUNNNCIOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO" + idAnuncio);

cliente.get("http://" + servidor + "/WebService/" + ficherophp, parametro, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
if (statusCode == 200) {
System.out.println("aqui dentro de 200!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
progressDialog.dismiss();
try {
JSONArray jsonArray = new JSONArray(new String(responseBody));
for (int i = 0; i < jsonArray.length(); i++) {
System.out.println("aqui dentro de FOR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
//aqui es donde rellenamos los campos obteniendo la info del JSON!
textoAnuncioNombre.setText(jsonArray.getJSONObject(i).getString("nombreNegocio"));
textoAnuncioLocalidad.setText(jsonArray.getJSONObject(i).getString("ProvinciaLocalidadBarrio_idLocalidad"));
//colocamos las horas de cada dia
if (jsonArray.getJSONObject(i).getString("LunesApertura2").equals(jsonArray.getJSONObject(i).getString("LunesCierre2")) && jsonArray.getJSONObject(i).getString("LunesApertura1").equals(jsonArray.getJSONObject(i).getString("LunesCierre1")) == false) {
textoAnuncioHoraLunes.setText("L:\t\t\t\t\t" + jsonArray.getJSONObject(i).getString("LunesApertura1").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("LunesCierre1").substring(0, 5));
} else if (jsonArray.getJSONObject(i).getString("LunesApertura2").equals(jsonArray.getJSONObject(i).getString("LunesCierre2")) && jsonArray.getJSONObject(i).getString("LunesApertura1").equals(jsonArray.getJSONObject(i).getString("LunesCierre1"))) {
textoAnuncioHoraLunes.setText("L:\t\t\t\t\tCERRADO");
} else {
textoAnuncioHoraLunes.setText("L:\t\t\t\t\t" + jsonArray.getJSONObject(i).getString("LunesApertura1").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("LunesCierre1").substring(0, 5) + ", " +
jsonArray.getJSONObject(i).getString("LunesApertura2").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("LunesCierre2").substring(0, 5));
}

if (jsonArray.getJSONObject(i).getString("MartesApertura2").equals(jsonArray.getJSONObject(i).getString("MartesCierre2")) && jsonArray.getJSONObject(i).getString("MartesApertura1").equals(jsonArray.getJSONObject(i).getString("MartesCierre1")) == false) {
textoAnuncioHoraMartes.setText("M:\t\t\t\t" + jsonArray.getJSONObject(i).getString("MartesApertura1").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("MartesCierre1").substring(0, 5));
} else if (jsonArray.getJSONObject(i).getString("MartesApertura2").equals(jsonArray.getJSONObject(i).getString("MartesCierre2")) && jsonArray.getJSONObject(i).getString("MartesApertura1").equals(jsonArray.getJSONObject(i).getString("MartesCierre1"))) {
textoAnuncioHoraMartes.setText("M:\t\t\t\tCERRADO");
} else {
textoAnuncioHoraMartes.setText("M:\t\t\t\t" + jsonArray.getJSONObject(i).getString("MartesApertura1").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("MartesCierre1").substring(0, 5) + ", " +
jsonArray.getJSONObject(i).getString("MartesApertura2").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("MartesCierre2").substring(0, 5));
}

if (jsonArray.getJSONObject(i).getString("MiercolesApertura2").equals(jsonArray.getJSONObject(i).getString("MiercolesCierre2")) && jsonArray.getJSONObject(i).getString("MiercolesApertura1").equals(jsonArray.getJSONObject(i).getString("MiercolesCierre1")) == false) {
textoAnuncioHoraMiercoles.setText("X:\t\t\t\t\t" + jsonArray.getJSONObject(i).getString("MiercolesApertura1").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("MiercolesCierre1").substring(0, 5));
} else if (jsonArray.getJSONObject(i).getString("MiercolesApertura2").equals(jsonArray.getJSONObject(i).getString("MiercolesCierre2")) && jsonArray.getJSONObject(i).getString("MiercolesApertura1").equals(jsonArray.getJSONObject(i).getString("MiercolesCierre1"))) {
textoAnuncioHoraMiercoles.setText("X:\t\t\t\t\tCERRADO");
} else {
textoAnuncioHoraMiercoles.setText("X:\t\t\t\t\t" + jsonArray.getJSONObject(i).getString("MiercolesApertura1").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("MiercolesCierre1").substring(0, 5) + ", " +
jsonArray.getJSONObject(i).getString("MiercolesApertura2").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("MiercolesCierre2").substring(0, 5));
}

if (jsonArray.getJSONObject(i).getString("JuevesApertura2").equals(jsonArray.getJSONObject(i).getString("JuevesCierre2")) && jsonArray.getJSONObject(i).getString("JuevesApertura1").equals(jsonArray.getJSONObject(i).getString("JuevesCierre1")) == false) {
textoAnuncioHoraJueves.setText("J:\t\t\t\t\t" + jsonArray.getJSONObject(i).getString("JuevesApertura1").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("JuevesCierre1").substring(0, 5));
} else if (jsonArray.getJSONObject(i).getString("JuevesApertura2").equals(jsonArray.getJSONObject(i).getString("JuevesCierre2")) && jsonArray.getJSONObject(i).getString("JuevesApertura1").equals(jsonArray.getJSONObject(i).getString("JuevesCierre1"))) {
textoAnuncioHoraJueves.setText("J:\t\t\t\t\tCERRADO");
} else {
textoAnuncioHoraJueves.setText("J:\t\t\t\t\t" + jsonArray.getJSONObject(i).getString("JuevesApertura1").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("JuevesCierre1").substring(0, 5) + ", " +
jsonArray.getJSONObject(i).getString("JuevesApertura2").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("JuevesCierre2").substring(0, 5));
}

if (jsonArray.getJSONObject(i).getString("ViernesApertura2").equals(jsonArray.getJSONObject(i).getString("ViernesCierre2")) && jsonArray.getJSONObject(i).getString("ViernesApertura1").equals(jsonArray.getJSONObject(i).getString("ViernesCierre1")) == false) {
textoAnuncioHoraViernes.setText("V:\t\t\t\t\t" + jsonArray.getJSONObject(i).getString("ViernesApertura1").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("ViernesCierre1").substring(0, 5));
} else if (jsonArray.getJSONObject(i).getString("ViernesApertura2").equals(jsonArray.getJSONObject(i).getString("ViernesCierre2")) && jsonArray.getJSONObject(i).getString("ViernesApertura1").equals(jsonArray.getJSONObject(i).getString("ViernesCierre1"))) {
textoAnuncioHoraViernes.setText("V:\t\t\t\t\tCERRADO");
} else {
textoAnuncioHoraViernes.setText("V:\t\t\t\t\t" + jsonArray.getJSONObject(i).getString("ViernesApertura1").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("ViernesCierre1").substring(0, 5) + ", " +
jsonArray.getJSONObject(i).getString("ViernesApertura2").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("ViernesCierre2").substring(0, 5));
}

if (jsonArray.getJSONObject(i).getString("SabadoApertura2").equals(jsonArray.getJSONObject(i).getString("SabadoCierre2")) && jsonArray.getJSONObject(i).getString("SabadoApertura1").equals(jsonArray.getJSONObject(i).getString("SabadoCierre1")) == false) {
textoAnuncioHoraSabado.setText("S:\t\t\t\t\t" + jsonArray.getJSONObject(i).getString("SabadoApertura1").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("SabadoCierre1").substring(0, 5));
} else if (jsonArray.getJSONObject(i).getString("SabadoApertura2").equals(jsonArray.getJSONObject(i).getString("SabadoCierre2")) && jsonArray.getJSONObject(i).getString("SabadoApertura1").equals(jsonArray.getJSONObject(i).getString("SabadoCierre1"))) {
textoAnuncioHoraSabado.setText("S:\t\t\t\t\tCERRADO");
} else {
textoAnuncioHoraSabado.setText("S:\t\t\t\t\t" + jsonArray.getJSONObject(i).getString("SabadoApertura1").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("SabadoCierre1").substring(0, 5) + ", " +
jsonArray.getJSONObject(i).getString("SabadoApertura2").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("SabadoCierre2").substring(0, 5));
}

if (jsonArray.getJSONObject(i).getString("DomingoApertura2").equals(jsonArray.getJSONObject(i).getString("DomingoCierre2")) && jsonArray.getJSONObject(i).getString("DomingoApertura1").equals(jsonArray.getJSONObject(i).getString("DomingoCierre1")) == false) {
textoAnuncioHoraDomingo.setText("D:\t\t\t\t\t" + jsonArray.getJSONObject(i).getString("DomingoApertura1").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("DomingoCierre1").substring(0, 5));
} else if (jsonArray.getJSONObject(i).getString("DomingoApertura2").equals(jsonArray.getJSONObject(i).getString("DomingoCierre2")) && jsonArray.getJSONObject(i).getString("DomingoApertura1").equals(jsonArray.getJSONObject(i).getString("DomingoCierre1"))) {
textoAnuncioHoraDomingo.setText("D:\t\t\t\t\tCERRADO");
} else {
textoAnuncioHoraDomingo.setText("D:\t\t\t\t\t" + jsonArray.getJSONObject(i).getString("DomingoApertura1").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("DomingoCierre1").substring(0, 5) + ", " +
jsonArray.getJSONObject(i).getString("DomingoApertura2").substring(0, 5) + "-" + jsonArray.getJSONObject(i).getString("DomingoCierre2").substring(0, 5));
}


textoAnuncioCondiciones.setText(jsonArray.getJSONObject(i).getString("condiciones"));
textoUbicacion.setText("C/" + jsonArray.getJSONObject(i).getString("Calle") + " " + jsonArray.getJSONObject(i).getString("Portal") + " " + jsonArray.getJSONObject(i).getString("Piso"));


String nombreImagen = (jsonArray.getJSONObject(i).getString("imagen"));
String urlfinal = "http://" + servidor + "/WebService/androidimg/uploads/" + nombreImagen;

descargarImagen(urlfinal);
latitud = Double.parseDouble(jsonArray.getJSONObject(i).getString("latitud"));
longitud = Double.parseDouble(jsonArray.getJSONObject(i).getString("longitud"));
System.out.println("LAAATITUDDDDDDDDDDDDDDDDDDDDDDDD" + latitud);
System.out.println("LONNNNGITUDDDDDDDDDDDDDDDDDDDDDDDD" + longitud);

}


} catch (JSONException e) {
Toast.makeText(MostrarinfoanuncioActivity.this, "ERROR IMPORTANTE, MANDANOS UN EMAIL Y LO SOLUCIONAREMOS. SECCIÓN CONTACTO", Toast.LENGTH_LONG).show();
}
}
}

@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
progressDialog.dismiss();
Toast.makeText(MostrarinfoanuncioActivity.this, "HA HABIDO ALGÚN PROBLEMA CON LA BUSQUEDA enoncreate, VUELVE A PROBAR EN UNOS INSTANTES", Toast.LENGTH_LONG).show();
}
});
//AQUI YA CARGADOS TODOS LOS DATOS DE TEXTO E IMAGEN. AHORRA VAMOS A CARGAR EL MAPA
try {
System.out.println("LAAATITUDDDDDDDDDDDDDDDDDDDDDDDD ANTES DEL SAVED" + latitud);
System.out.println("LONNNNGITUDDDDDDDDDDDDDDDDDDDDDDDD ANTES DEL SAVED" + longitud);
mapView.onCreate(savedInstanceState);
System.out.println("LAAATITUDDDDDDDDDDDDDDDDDDDDDDDD DESPUES DEL SAVED" + latitud);
System.out.println("LONNNNGITUDDDDDDDDDDDDDDDDDDDDDDDD DESPUES DEL SAVED" + longitud);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
Toast.makeText(MostrarinfoanuncioActivity.this, "Latitud desde oncreate()"+ latitud, Toast.LENGTH_LONG).show();
Toast.makeText(MostrarinfoanuncioActivity.this, "Longitud desde oncreate()"+ longitud, Toast.LENGTH_LONG).show();
LatLng latlng = new LatLng(latitud, longitud);
googleMap.addMarker(new MarkerOptions().position(latlng));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 18));

}


});
}catch (Exception e){
Toast.makeText(MostrarinfoanuncioActivity.this, "HA HABIDO ALGÚN PROBLEMA CON EL MAPA en oncreate, VUELVE A PROBAR EN UNOS INSTANTES. SI PERSISTE ENVÍANOS UN EMAIL. SECCIÓN CONTACTO", Toast.LENGTH_LONG).show();
}

}

















[b]EJECUCION EN ANDROID MONITOR
10-26 22:49:17.341 19820-19820/proyecto.ejemplo.mundo.hola.victor.proyecto I/System.out: LAAATITUDDDDDDDDDDDDDDDDDDDDDDDD ANTES DEL SAVED0.0
10-26 22:49:17.341 19820-19820/proyecto.ejemplo.mundo.hola.victor.proyecto I/System.out: LONNNNGITUDDDDDDDDDDDDDDDDDDDDDDDD ANTES DEL SAVED0.0
10-26 22:49:18.071 19820-19820/proyecto.ejemplo.mundo.hola.victor.proyecto I/System.out: LAAATITUDDDDDDDDDDDDDDDDDDDDDDDD DESPUES DEL SAVED0.0
10-26 22:49:18.071 19820-19820/proyecto.ejemplo.mundo.hola.victor.proyecto I/System.out: LONNNNGITUDDDDDDDDDDDDDDDDDDDDDDDD DESPUES DEL SAVED0.0


que

10-26 22:49:18.141 19820-19820/proyecto.ejemplo.mundo.hola.victor.proyecto I/System.out: aqui dentro de 200!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10-26 22:49:18.151 19820-19820/proyecto.ejemplo.mundo.hola.victor.proyecto I/System.out: aqui dentro de FOR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10-26 22:49:18.171 19820-19820/proyecto.ejemplo.mundo.hola.victor.proyecto I/System.out: LAAATITUDDDDDDDDDDDDDDDDDDDDDDDD41.6537353
10-26 22:49:18.171 19820-19820/proyecto.ejemplo.mundo.hola.victor.proyecto I/System.out: LONNNNGITUDDDDDDDDDDDDDDDDDDDDDDDD-0.8767757[/b]

En client.get... obtengo la latitud y la longitud de una base de datos en un servidor y funciona perfectamente.

Mi problema es que el client.get se ejecuta más tarde y no entiendo el porqué si está escrito encima. Eso hace que no reciba la latitud y la longitud correctamente siempre.

Gracias por la ayuda. Un saludo a todos!

octubre 26, 2016 | Unregistered CommenterVictor

Hola, eso que indicas, te esta ocurriendo, porque el objeto AsyncHttpClient, es un cliente que realiza peticiones asíncronas, es decir, que levanta un nuevo thread para realizar la peticion dejando libre el thread principal para que continue su ejecución.

El AsyncHttpClient.get(...) funciona de la siguiente forma, si la petición a tenido éxito, se ejecuta el método onSuccess(...) y si por el contrario, la petición a fallado o algo, se ejecuta el metodo onFailure(...).

Con todo esto, creo... que para solucionar tu problema, la carga del mapa, debes hacerla dentro del cuerpo del método onSuccess(...){ ... }, ya que es cuando la petición tiene éxito, es cuando se deben cargar los datos en el mapa.
Tal y como lo estas haciendo, lo que te esta ocurriendo, es que estas intentando cargar los datos en el mapa antes de que la petición del cliente AsyncHttpClient se haya procesado.

Bueno, prueba eso, a cargar el mapa en las ultimas lineas del método onSuccess en lugar de ponerlo directamente en el onCreate como lo estas haciendo y ya nos cuentas. Un saludo

octubre 27, 2016 | Registered Commenterloderain

Funciona!!! el problema era que no entendía lo que era algo asíncrono.

Bueno ni tan siquiera me había parado a pensar en ello.

Junto a lo que me has escrito más cosas que he leído en google he llegado a entenderlo.

¡Muchísimas gracias por tu aporte! no sabes la de tiempo que he dedicado a resolver este problema y tu solución ha funcionado a la primera :)

Un saludo compañero!

octubre 27, 2016 | Unregistered CommenterVictor