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
  • 974
  • 1
  • 7
  • 25
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,230
  • 1
  • 27
  • 51
0

This is the solution 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 ( );
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
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
  • 730
  • 11
  • 20