简体   繁体   中英

how to POST raw type JSON Data in Body?

I'm sending a post request to server using Volley request and i have some raw Type JSON data that has to be sent. Here I have no idea how to send the 4th object students which is type of Array.

JSON data to be posted is

{
"course_id":1,
"batch_id":1,
"subject_id":1,
"students":[{"student_id":6,"present":0},{"student_id":17,"present":0}] 
}

My code

private void fetchDataAndMarkAttendence() {
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                status = jsonObject.getString("status");
            } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }){
        @Override
       public Map<String, String> getHeaders() throws AuthFailureError {
           HashMap<String, String> map = new HashMap<String, String>();
           SharedPreferences prefs = getApplicationContext().getSharedPreferences(MY_PREFS_NAME, Activity.MODE_PRIVATE);
                token = prefs.getString("token","");
                map.put("Authorization", "Token "+token);
                return map;
            }
        }
    }

So, any help regarding how can i post the JSON data mentioned above will be helpfull.

This is called JSONArray.

try {

        JSONArray jsonArray = jsonObject.getJSONArray();

        for (int i = 0; i < jsonArray.length(); i++) {
            int student_id = jsonObject.getInt("student_id");
            int present = jsonObject.getInt("present");
        }

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

EDIT

as mention, you want to post the json. So create the json suing below code and post mainJsonObject.toString(); as parameter.

 try {

        JSONObject mainJsonObject = new JSONObject();

        JSONArray jsonArray = new JSONArray();
        //if more than one then wrap inside loop
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("student_id", value);
        jsonObject.put("present", value);
        jsonArray.put(jsonObject);
        //end loop

        mainJsonObject.put("course_id", value);
        mainJsonObject.put("batch_id", value);
        mainJsonObject.put("subject_id", value);
        mainJsonObject.put("students", jsonArray);

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

hope it helps.

Replace your StringRequest with JsonObjectRequest

JSONObject postparams = new JSONObject();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, postparams,
new Response.Listener() {
    @Override
    public void onResponse(JSONObject response) {
        //Success Callback
    }
},
new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        //Failure Callback
    }
});

can you try this? This probably will fix your problem.

RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        try {
            JSONObject jsonObject = new JSONObject(response);
            status = jsonObject.getString("status");
        } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    }){
    @Override
    public byte[] getBody() throws AuthFailureError {
        String yourJSON = yourJsonObj.toString() ;
        return yourJSON.getBytes();
    }
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
         HashMap<String, String> map = new HashMap<String, String>();
         SharedPreferences prefs = getApplicationContext().getSharedPreferences(MY_PREFS_NAME, Activity.MODE_PRIVATE);
              token = prefs.getString("token","");
              map.put("Authorization", "Token "+token);
              return map;
   }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM