VOLEY, sending all the data from a SQLite table

2

I want to send all the data of the SQLite database in the following way:

@Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> parameters = new HashMap<String, String>();
            SQLiteDatabase db = basededatos.getReadableDatabase();
            Cursor cursor = db.query("aguacate", new String[] {"calibre","precio","porcentaje","resultado",},null, null, null, null, null);
            if (cursor.moveToFirst()) {
                //Recorremos el cursor hasta que no haya más registros
                do {
                    parameters.put("calibre",cursor.getString(1));
                    parameters.put("precio", cursor.getString(2));
                    parameters.put("porcentaje", cursor.getString(3));
                    parameters.put("resultado", cursor.getString(4));
                } while(cursor.moveToNext());
            }
            return parameters;
        }
    };
    requestQueue.add(request);

But, how can I put the

requestQueue.add(request);

inside the while cycle? It does not leave me, is there any solution?

    
asked by DoubleM 10.12.2016 в 05:02
source

1 answer

2

Within the cycle while will not leave you because it is inside the section where the parameters that will be sent in StringRequest

are assigned

As I understand your question, you need to make a request by means of the StringRequest for each record of your query, for which I recommend that you first do a function that iterates your query, consume another function that is called with each record found, for which I would propose something like this:

Code of the function that generates the results for each request

public void ProcessQuery(){
    SQLiteDatabase db = basededatos.getReadableDatabase();
    Cursor cursor = db.query("aguacate", new String[] {"calibre","precio","porcentaje","resultado",},null, null, null, null, null);
    if (cursor.moveToFirst()) {
        //Recorremos el cursor hasta que no haya más registros
        do {
            final Map<String, String> parameters = new HashMap<String, String>();
            parameters.put("calibre",cursor.getString(1));
            parameters.put("precio", cursor.getString(2));
            parameters.put("porcentaje", cursor.getString(3));
            parameters.put("resultado", cursor.getString(4));
            //Por cada registro encontrado se obtienen los parámetros y se envían a la función que procesa la petición por medio del StringRequest
            ProcessRequest(parameters);
        } while(cursor.moveToNext());
    }
}

Code of the function that executes the requests

private void ProcessRequest(Map<String, String> parameters){
    StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    //Showing toast message of the response
                    Toast.makeText(upload.this, s , Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    //Showing toast
                    Toast.makeText(upload.this, volleyError.getMessage(), Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            return parameters;
        }
    };

    //Creating a Request Queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    //Adding request to the queue
    requestQueue.add(stringRequest);
}

I also recommend that you perform the handling of variables in case one is null and no exception is generated

Update

The varible parameters is declared as final so that the code does not mark errors

    
answered by 10.12.2016 / 07:39
source