Estoy aprendiendo a crear aplicaciones en android,esta es para un galpón, bueno para ayudara a mi padre a tener un control de cuyes en fin , pero trate de colocar un Response.Listener dentro de otro, en la ejecución logra cargar los datos a la base de datos, pero me bota un error el cual no permite ejecutar el Intent para regresar al menú.
W/System.err: org.json.JSONException: Value 2472434902019-01-10 of type java.lang.String cannot be converted to JSONObject
W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
W/System.err: at org.json.JSONObject.<init>(JSONObject.java:163)
W/System.err: at org.json.JSONObject.<init>(JSONObject.java:176)
...
W/System.err: at com.example.guido.proyecto.registro$2$1$1.onResponse(registro.java:112)
W/System.err: at com.example.guido.proyecto.registro$2$1$1.onResponse(registro.java:108)
No se que andará mal aquí mi código:
btnrcuyguardar=(Button) findViewById(R.id.btnregcuygua);
btnrcuyguardar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//definimos variables
final int cant_mac=Integer.parseInt(etmac.getText().toString());
final int cant_hem=Integer.parseInt(ethem.getText().toString());
final String col_jaula=spjcol.getSelectedItem().toString();
final int fil_jaula=Integer.parseInt(spjfil.getSelectedItem().toString());
final String tip_cuy=sptcuy.getSelectedItem().toString();
Response.Listener<String> responseListiner=new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse=new JSONObject(response);
boolean success= jsonResponse.getBoolean("success");
if(success){
DateFormat dt=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
final String date =dt.format(Calendar.getInstance().getTime());
final String busca_col_jaula=jsonResponse.getString("col_jaula");
final int busca_fil_jaula=jsonResponse.getInt("fil_jaula");
final String busca_tip_cuy=jsonResponse.getString("tip_cuy");
final int before_cant_mac=jsonResponse.getInt("cant_mac");
final int before_cant_hem=jsonResponse.getInt("cant_hem");
int suma_machos=cant_mac+before_cant_mac;
int suma_hembra=cant_hem+before_cant_hem;
final int new_total=suma_machos+suma_hembra;
final String after_Fecha_send=String.valueOf(date);
**linea 108 Response.Listener<String> responseListiner = new Response.Listener<String>() {**
@Override
public void onResponse(String response) {
try {
**linea 112 JSONObject jsonResponse1=new JSONObject(response);**
boolean success = jsonResponse1.getBoolean("success");
if(success){
Intent intent= new Intent(registro_cuyes.this,menu.class);
registro_cuyes.this.startActivity(intent);
}else{
AlertDialog.Builder builder=new AlertDialog.Builder(registro_cuyes.this);
builder.setMessage("Error al Actualizar")
.setNegativeButton("Retry",null)
.create().show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
ActualizarCantRequest actualizarCantRequest=new ActualizarCantRequest(suma_machos,suma_hembra,new_total,after_Fecha_send,busca_col_jaula,busca_fil_jaula,busca_tip_cuy,responseListiner);
RequestQueue queue1=Volley.newRequestQueue(registro_cuyes.this);
queue1.add(actualizarCantRequest);
}else{
AlertDialog.Builder builder=new AlertDialog.Builder(registro_cuyes.this);
builder.setMessage("Fila/Columna no encontrada")
.setNegativeButton("Retry",null)
.create().show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
AgregarCantRequest agregarCantRequest=new AgregarCantRequest(col_jaula,fil_jaula,tip_cuy,responseListiner);
RequestQueue queue=Volley.newRequestQueue(registro_cuyes.this);
queue.add(agregarCantRequest);
}
});
Con respecto a la solución brindada por @Jorgesys: R:Lo que realice fue comentar la linea despues del segundo response y coloque un mensaje de alerta y ahora funciona muy bien.
Response.Listener<String> responseListiner=new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//aporte Creo un dialogo para que informe que el regitro fue exitoso!!!
AlertDialog.Builder builder=new AlertDialog.Builder(registro_cuyes.this);
builder.setMessage("Exito al actualizar")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent addcuy=new Intent(registro_cuyes.this,menu.class);
registro_cuyes.this.startActivity(addcuy);
}
})
.create().show();
/*try {
JSONObject jsonObject=new JSONObject(response);
boolean listo=jsonObject.getBoolean("listo");
if(listo){
Intent update=new Intent(registro_cuyes.this,menu.class);
registro_cuyes.this.startActivity(update);
}else{
AlertDialog.Builder builder=new AlertDialog.Builder(registro_cuyes.this);
builder.setMessage("Error al Actualizar")
.setNegativeButton("Retry",null)
.create().show();
}
} catch (JSONException e) {
e.printStackTrace();
}*/
}
};
ActualizarCantRequest actualizarCantRequest=new ActualizarCantRequest(suma_machos,suma_hembra,new_total,after_Fecha_send,busca_col_jaula,busca_fil_jaula,busca_tip_cuy,responseListiner);
RequestQueue queue1=Volley.newRequestQueue(registro_cuyes.this);
queue1.add(actualizarCantRequest);
response
dentro deonResponse