0

I Want to Post My Data In This Format In My Json

Json Format is not Match with my format which I post,that why it return false in reponse

{
    "expense" : {
        SiteId as Int
        ItemName as string
        Amount as int
        PaidBy as string,
        Id  -- case of Edit
    },
    "expenseConsumers":[ //Array Start
        {
            "Id": 1,
            "Name": "vick",
            "SiteId": 8,
            "ExpenseConsumerID": 0,
            "isChecked": false
        },
        {
            "Id": 1,
            "Name": "vick",
            "SiteId": 8,
            "ExpenseConsumerID": 0,
            "isChecked": false
        }
    ] //Array End......
}

But Problem is Some where in code. I can't able to Solve.

My Code

The problem is in getParms() function maybe it not post the format.

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_SAVEEXPENSES,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                System.out.println("save Expense  success : " + response.toString());
                // progressBar.setVisibility(View.GONE);
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getContext(), "Volley Error! " + error.toString(), Toast.LENGTH_SHORT).show();
                System.out.println("Volley Error! : " + error.toString() + " and " + error.getMessage());
                // progressBar.setVisibility(View.GONE);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> parms = new HashMap<>();
                JSONObject js = new JSONObject();
                JSONObject  jsonobject_one = new JSONObject();
                JSONArray    jsonArray=new JSONArray();

                try {
                    jsonobject_one.put("SiteId", Integer.parseInt(sharedPreferences.getString("siteId", "0")));
                    jsonobject_one.put("ItemName", "sabji");
                    jsonobject_one.put("Amount", 560);
                    jsonobject_one.put("PaidBy", 35);
                    jsonobject_one.put("Id", 0);

                    for (int iteration =0;iteration<allConsumerArray.size();iteration++) {
                        JSONObject jsonobject_TWO = new JSONObject();
                        jsonobject_TWO.put("Id", 35);
                        jsonobject_TWO.put("Name", "rana");
                        jsonobject_TWO.put("SiteId", Integer.parseInt(sharedPreferences.getString("siteId","0")));
                        jsonobject_TWO.put("ExpenseConsumerID", 0);
                        jsonobject_TWO.put("isChecked", true);
                        jsonArray.put(jsonobject_TWO);
                    }
                    parms.put("expenseConsumers", jsonArray.toString());

                    parms.put("expense", jsonobject_one.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return parms;
            }
        };

RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);

Here the problem is in getParms function.

Community
  • 1
  • 1
  • u have to put the "expense" and "expenseConsumers" in the **js** objection then put the **js** object in your params map. – Ahmed Garhy Jun 14 '18 at 19:21

1 Answers1

0

i don't see you assigning "jsonobject_one" and "jsonArray" to "js". your final json formatted data should compelete with :

js.put("expense", jsonobject_one);
js.put("expenseConsumers", jsonArray);
Nickan
  • 466
  • 5
  • 17
  • then what would be return in parm – Harwinder Singh Jun 14 '18 at 19:20
  • I didn't use volley library , I'm using retrofit and in my opinion retrofit is much better and simpler than volley . BTW have you seen this link before ? https://stackoverflow.com/questions/46422727/how-to-send-json-array-as-post-request-in-volley?noredirect=1&lq=1 – Nickan Jun 14 '18 at 19:31