Send Json by Volley

1

I need to send something similar to this:

"nuevo": [
    {
    "idusuario": "1",
    "idcuenta": "2",
    "calidades": [
        {
            "id": "1",
            "peso": "100",
            "cajas": "5"
        },
    ]
}
]

Do I need to send it via post RAW as I can do it with volley ?

 private class nuevo extends AsyncTask<Void, Void, Void> {
        public void onPreExecute() {

        }
        public void onPostExecute(Void unused) {
        }

        @Override
        protected Void doInBackground(Void... params) {
            StringRequest request = new StringRequest(Request.Method.POST, getString(R.string.entry_nueva), new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> parameters = new HashMap<String, String>();

                    return parameters;
                }
            };
            requestQueue.add(request);
            return null;
        }
    }
    
asked by DoubleM 14.06.2018 в 01:51
source

1 answer

1

Try something like that, the important thing is to pass the content-type to it.

private class nuevo extends AsyncTask<Void, Void, Void> {

    public void onPreExecute() { }

    public void onPostExecute(Void unused) { }

    @Override
    protected Void doInBackground(Void... params) {

       JSONObject jsonBody = new JSONObject("TU JSON EN STRING");

       StringRequest request = new StringRequest("TU URL EN STRING", jsonBody, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                }) {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> parameters = new HashMap<String, String>();
                        parameters.put("Content-Type", "application/json");
                        return parameters;
                    }
                };
                requestQueue.add(request);
                return null;
            }
        }

And a little help to shape the JSON using Google's GSON library.

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;


public static JsonObject createJSON(){

    JsonObject jsonParent = new JsonObject();
    jsonParent.addProperty("idusuario", "1";
    jsonParent.addProperty("idcuenta", "2";

    JsonArray jsonCalidadesArray = new JsonArray();

    for (Recorrer array de objetos "Calidad" que tengas) {
        JsonObject jsonCalidad = new JsonObject();
        jsonCalidad.addProperty("id", "1";
        jsonCalidad.addProperty("peso", "100";
        jsonCalidad.addProperty("cajas", "5";
        jsonCalidadesArray.add(jsonCalidad);
    }
    jsonParent.add("calidades",  jsonCalidadesArray);
    return jsonParent;
}
    
answered by 14.06.2018 в 10:50