I have a problem with my Android Studio code. My application is based on a date picker where when you select a date it will populate a table with a database query and populate a table with the results. The code for my onCreate method is as follows. (Edited based on A.Cedano's answer)
calendario.init(0, 0, 0, new DatePicker.OnDateChangedListener(){
@Override
public void onDateChanged (DatePicker datePicker, int año, int mes, int dia){
String username = i.getStringExtra("us_usuario");
String password = i.getStringExtra("us_clave");
int Año = calendario.getYear();
int Mes = calendario.getMonth()+1;
int Dia = calendario.getDayOfMonth();
Response.Listener<String> responseListener = new Response.Listener<String>(){
@Override
public void onResponse(String response){
try{
JSONObject jsonObject = new JSONObject(response);
String hora_inicio = jsonObject.getString("hora_inicio");
String hora_fin = jsonObject.getString("hora_fin");
String fecha = jsonObject.getString("fecha");
String nombre = jsonObject.getString("nombre");
String apellidos = jsonObject.getString("apellidos");
String prestacion = jsonObject.getString("prestacion");
int[] comprobarFecha = separarFecha(fecha);
String [] arrayRespuesta = {hora_inicio, hora_fin, fecha, nombre, apellidos, prestacion};
if (comprobarFecha[0] == calendario.getYear() && comprobarFecha[1] == calendario.getMonth() && comprobarFecha[2] == calendario.getDayOfMonth()){
tablePaciente.removeAllViews();
TablaPacientes tabla = new TablaPacientes(Usuario.this, tablePaciente);
cargarTabla(tabla, arrayRespuesta);
} else {
Toast t = Toast.makeText(getApplicationContext(), "No hay pacientes para esta fecha", Toast.LENGTH_LONG);
t.show();
}
}catch(JSONException e){
AlertDialog.Builder builder = new AlertDialog.Builder(Usuario.this);
builder.setMessage("Excepción en el JSON "+e.getMessage())
.setNegativeButton("Retry", null)
.create().show();
}catch(ArrayIndexOutOfBoundsException e){
AlertDialog.Builder builder = new AlertDialog.Builder(Usuario.this);
builder.setMessage("Error de excepción en el array "+e.getLocalizedMessage())
.setNegativeButton("Retry", null)
.create().show();
}
}
};
String fecha = Año+"-"+Mes+"-"+Dia;
RegisterRequest registerRequest = new RegisterRequest(username, password, fecha, responseListener);
RequestQueue queue = Volley.newRequestQueue(Usuario.this);
queue.add(registerRequest);
}
});
The error I get when this activity is opened is the following:
Any idea why it could be? This code is tested in another activity and it works, but this is the first time I try to put it inside an onDateChanged
This is my json output for DONATE user, DONATE key and date "2017-10-26":
{
"success": true,
"hora_inicio": "14:00",
"hora_fin": "14:15",
"fecha": "2017-10-26",
"nombre": "JUAN MANUEL",
"apellidos": "LLORENTE RODRIGO",
"prestacion": "REVISION OFTALMOLOGICA"
}
And this is the php code of my application:
Any possible solution please? In the json it is clearly seen that start_time does give a response, but nevertheless it does not seem to pick it up. Why could it be?
If the JSON returns it to you as you have it in the question, we are dealing with a
JSONObject
, not aJSONArray
.Then:
You should create a
JSONObject
fromresponse
and not aJSONArray
.You won't need the
for
if it's a single JSON object. If there are several, you will use the for, but counting the values of theJSONObject
, not theJSONArray
.In the JSON you display the values are not organized as you intend to read them. For example, the name does not come in a key called
nombre
but rather the key of the name is called4
. Means this won't work:String nombre = jsonObject.getString("nombre");
for the name. Instead this yes:String nombre = jsonObject.getString("4");
. The same goes for the other values. And also,hora_inicio
and other relatives don't come in an array... I don't know why you try to read them like this:when those values come as plain keys in the JSON.
It is evident that the construction of your JSON is not being done properly.
Anyway, a corrected code (for the JSON as you have it now) would be:
The supposed value of
arrayRespuesta[2]
would actually be obtained like this:Making those changes the code will work if there are no other errors. But it is not desirable. I think you should review the JSON at the source so that it is built with friendly key names and not numbers.