First of all, I want to thank you for any kind of help you can give me, it is the first time that I am trying to bring data from a server (MySQL) and save it in my application with SQLite, I have seen several tutorials and I have understood something; I have an error that, as indicated in the Debug , does not allow me to change the type of JSON; I understand that in stringRequest , which is the StringRequest of Volley, it brings the query from the server and then in onResponse I go through the response, but this is where I get the error and I don't know very well what it refers to.
This is my code:
public void SincronizarCodigos(Context context){
if(checkConexion(this)){
// abrir conexión
ConexionSQLiteHelper conex = new ConexionSQLiteHelper(this,"bd_datos",null,1);
//Abrir base da datos en modo escritura
final SQLiteDatabase db = conn.getWritableDatabase();
String url = "http://192.168.0.131/servicio_web/web/obtener_gastos.php";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray array = response.getJSONArray("gastos");
for(int i = 0; i<array.length(); i++)
{
//JSONObject permite obtener un elemento dentro del JSONarray
JSONObject object = array.getJSONObject(i);
conex.GuardarCodigos(
object.getString("idOperario"),
object.getString("Operario"),
object.getString("bloque"),
object.getString("supervisor"),db);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("error",""+error);
}
});
MySingleton.getInstance(context).addToRequestQue(jsonObjectRequest);
}
}
And this is the error that marks me in the Debug:
W/System.err: org.json.JSONException: Value {"estado":1,"gastos":[{"idOperario":"001","Operario":"OPERARIO 1","bloque":"BQ 001","supervisor":"SUPERVISOR 1"},{"idOperario":"002","Operario":"OPERARIO 2","bloque":"BQ 002","supervisor":"SUPERVISOR 2"},{"idOperario":"003","Operario":"OPERARIO 3","bloque":"BQ 003","supervisor":"SUPERVISOR 3"},{"idOperario":"004","Operario":"OPERARIO 4","bloque":"BQ 004","supervisor":"SUPERVISOR 4"},{"idOperario":"005","Operario":"OPERARIO 4","bloque":"BQ 005","supervisor":"SUPERVISOR 5"}]} of type org.json.JSONObject cannot be converted to JSONArray
W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONArray.<init>(JSONArray.java:96)
at org.json.JSONArray.<init>(JSONArray.java:108)
at com.example.formsaide.MainActivityConsultaCodigos$2.onResponse(MainActivityConsultaCodigos.java:114)
at com.example.formsaide.MainActivityConsultaCodigos$2.onResponse(MainActivityConsultaCodigos.java:110)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82)
W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
at android.os.Handler.handleCallback(Handler.java:819)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:210)
at android.app.ActivityThread.main(ActivityThread.java:5943)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:817)
The error you show says that the JSON string is of type JSONObject and cannot be converted to JSONArray , the JSON string you have is also shown:
It is actually an Object and not an array, I imagine that what you want is to load the expenses, for this you must first obtain the value as an object and then access the " expenses " property that you have in the object.
You must get the value as JSONObject using JsonObjectRequest , here is an example:
Here is the documentation for you to take a look at.
Firstly you are missing a parameter when making the request, it must be:
Now if you check the error message and your response indicates that you are trying to cast a value of type
JSONObject
toJSONArray
Remember that the information
JSON
can be defined as:If the json starts with
{
it is considered as a Json object.If the json starts with
[
it is considered as a Json array.and this is your answer, a
JSONObject
that contains aJSONArray
and inside severalJSONObject
:Therefore you must first get the
JSONObject
and then get the so-JSONArray
called "expenses" to iterate through each of itsJSONObject
and save its values in the database: