0

I want to send json data to the server but I am not able to do it. I have pasted my payload of json data below please check it and help me. In my Json Data payload so many array and object which creates a problem to me send the server. I have checked all the post in google but not able to do it.

//payload

{
"action":"create",
"machinetypelist":[{"id":"","materialTypeId":"1","machineinplantid":"MIPID-103","material":["1","2"]}]
}

4 Answers4

0

You can use Gson to send json data from your model.

Amrish Kakadiya
  • 934
  • 1
  • 7
  • 23
0
//escape the double quotes in json string
String payload="{\"action\":\"create\",\"machinetypelist\":[{\"id\":\"\",\"materialTypeId\":\"1\",\"machineinplantid\":\"MIPID-103\",\"material\":[\"1\",\"2\"]}]}"
String requestUrl="your url";
sendPostRequest(requestUrl, payload);

create sendPostRequest method. This will work. I refered this link

Ashish Kudale
  • 1,166
  • 1
  • 24
  • 50
0
This is the sollution for my question-
JSONObject js = new JSONObject ();
            try {
                js.put ("action","create");
                JSONObject jsonObject = new JSONObject ();
                jsonObject.put ("id","");
                jsonObject.put ("materialTypeId","");
                jsonObject.put ("machineinplantid","");
                JSONArray jsonArray = new JSONArray ();
                jsonArray.put ();
                jsonObject.put ("material",jsonArray);
                JSONArray jsonArray1 = new JSONArray ();
                jsonArray1.put (jsonObject);
                js.put ("machinetypelist",jsonArray1);

            } catch (JSONException e) {
                e.printStackTrace ( );
            }
0

By using Volley you can send JSON data as request like below, i hope this will help you to understand how to send JsonObjectRequest.

private void sendJsonData(JSONObject jsonObjectRequest) {

    String url = "your_url";

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObjectRequest,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {

                    Log.d("TAG", "onResponse: get your response here ");
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Log.d("TAG", "onErrorResponse: ERROR");
        }
    }) {

    };
    AppController.getInstance().addToRequestQueue(jsonObjectRequest);

}
yatin deokar
  • 652
  • 11
  • 19