Sending Json objects inside an array to the server

I am very new to this and I am trying to send dynamic objects inside an array via volley.You can check this at http://www.jsoneditoronline.org/ . I was looking for coding, so any help would be greatly appreciated. Below is a snippet of my code in which objects will be dynamic if I select from spinner and send them to the server

[
   {
      "longDescription":"Carrot Cheese Paratha is perfect to attract kids as it is colorful and yummy...",
      "productName":"CARROT CHEESE PARATHA",
      "name":"Delicious Kids Meal",
      "productId":"Monday",
      "catalogName":"KIDS TIFFIN MENU",
      "categoryName":"Monday",
      "subCategoryName":"Long Break",
      "kidId":47
   },
   {
      "longDescription":"Freshly cooked Desi Ghee Paratha along with Paneer butter masala",
      "productName":"Paratha plus paneer butter masala",
      "name":"Delicious Kids Meal",
      "productId":"Monday",
      "catalogName":"KIDS TIFFIN MENU",
      "categoryName":"Monday",
      "subCategoryName":"Short Break",
      "kidId":47
   },
   {
      "longDescription":"Basmati Rice along with freshly cooked Matar Paneer (cottage Cheese)",
      "productName":"Matar paneer and Basmati Rice",
      "name":"Delicious Kids Meal",
      "productId":"Wednesday",
      "catalogName":"KIDS TIFFIN MENU",
      "categoryName":"Wednesday",
      "subCategoryName":"Short Break",
      "kidId":47
   }
]
+4
source share
2 answers

You can do it with JsonArrayRequest

RequestQueue requestQueue = Volley.newRequestQueue(this);
    final String jsonString = "your json here";
    try {
        JSONArray jsonArray = new JSONArray(jsonString);
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, url, jsonArray, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                //
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //
            }
        });
        requestQueue.add(jsonArrayRequest);
    } catch (JSONException e) {
        e.printStackTrace();
    }
+1
source

Class model.

public class test {

/**
 * longDescription : Carrot Cheese Paratha is perfect to attract kids as it is colorful and yummy...
 * productName : CARROT CHEESE PARATHA
 * name : Delicious Kids Meal
 * productId : Monday
 * catalogName : KIDS TIFFIN MENU
 * categoryName : Monday
 * subCategoryName : Long Break
 * kidId : 47
 */

private String longDescription;
private String productName;
private String name;
private String productId;
private String catalogName;
private String categoryName;
private String subCategoryName;
private int kidId;

public String getLongDescription() {
    return longDescription;
}

public void setLongDescription(String longDescription) {
    this.longDescription = longDescription;
}

public String getProductName() {
    return productName;
}

public void setProductName(String productName) {
    this.productName = productName;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getProductId() {
    return productId;
}

public void setProductId(String productId) {
    this.productId = productId;
}

public String getCatalogName() {
    return catalogName;
}

public void setCatalogName(String catalogName) {
    this.catalogName = catalogName;
}

public String getCategoryName() {
    return categoryName;
}

public void setCategoryName(String categoryName) {
    this.categoryName = categoryName;
}

public String getSubCategoryName() {
    return subCategoryName;
}

public void setSubCategoryName(String subCategoryName) {
    this.subCategoryName = subCategoryName;
}

public int getKidId() {
    return kidId;
}

public void setKidId(int kidId) {
    this.kidId = kidId;
}

}

The VolleyJsonPost method from the VolleyConnection class that I wrote:

public static void jsonPost(Context context, String url, JSONObject jsonObject, Response.Listener<JSONObject> responceListener, Response.ErrorListener errorListener)
{
    JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST,url,jsonObject,responceListener,errorListener);

    Volley.newRequestQueue(context).add(jsonRequest);
}

and you use this method:

VolleyConnection.jsonPost(context, rootUrl, jsonObject, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {


            try {
                // do stuff
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });

}
0
source

Source: https://habr.com/ru/post/1650634/


All Articles