send data using JsonArrayRequest using POST in android Volley

0

I am new to that in the use of JsonArrayRequest or JsonObjectRequest , using the library Android Volley .

I'm doing an Android login when I try to send the data through a post that does not get it from the web service, but I do not send it.

public void validateUser(){
    //Obtenemos el usuario y contraseña de login
    final String userName = usuario.getText().toString().trim();
    final String passwordField = password.getText().toString().trim();
    queue = requestSingleton.getInstance(this.getApplicationContext()).getRequestQueue();
    //hacemos el login
    progressBar.setMessage("Iniciado sesión");
    progressBar.show();
    final JsonArrayRequest jsonObjectRequest = new JsonArrayRequest
            (Request.Method.POST,url,null, new Response.Listener<JSONArray>(){

                @Override
                public void onResponse(JSONArray response) {
                    try {
                        progressBar.dismiss();
                        JSONObject objeto = response.getJSONObject(0);
                        Toast.makeText(MainActivity.this, "Respuesta del server:"+objeto.getString("usuario"), Toast.LENGTH_SHORT).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO Auto-generated method stub
                    progressBar.dismiss();
                    Toast.makeText(MainActivity.this, "Error en la conexión"+error.getMessage(),Toast.LENGTH_SHORT).show();
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> params = new HashMap<>();
            //enviamos parametros a la peticion
            params.put(KEY_USER, userName);
            params.put(KEY_PASSWORD, passwordField);
            //retornamos los parametros
            return params;
        }
    };


    //agregamos la peticion
    queue.add(jsonObjectRequest);

}

The detail is using JsonArrayRequest or JsonObjectRequest , because when I use StringRequest

    
asked by Miguel Osorio 18.10.2016 в 22:13
source

1 answer

1

In the code snippet:

new JsonArrayRequest
        (Request.Method.POST,url,null, new Response.Listener<JSONArray>(){

declare an empty JSONArray beforehand and put it as a parameter where the null is. The same thing happened to me and by placing an empty element I was able to solve it.

    
answered by 19.10.2016 в 22:55