Search code examples
androidjsonandroid-volleyjsonobjectrequest

How to send {data:[{id:"11:}]} request to server in using volley and pass ObjectRequest in body


I want to pass {"data:"[{"id":"12"}]} in volley request body.

private void reqrej(final String currentLat) {


    String insertData = "http://www.xxxxxxxx.com/makeinforequest.php";
    final StringRequest stringRequest = new StringRequest(Request.Method.POST, insertData, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            Log.i("json", response.toString());

            tv.setText(response.toString());

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {

            //  String mylong = String.valueOf(mCurrentLocation.getLongitude()).toString();

            Map<String, String> params = new HashMap<String, String>();
            params.put("data", "");


           //where I can type request code?

            return checkParams(params);


        }

        private Map<String, String> checkParams(Map<String, String> map) {
            Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, String> pairs = (Map.Entry<String, String>) it.next();
                if (pairs.getValue() == null) {
                    map.put(pairs.getKey(), "");
                }
            }
            return map;
        }
    };

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


}

please guide me how to pass this things in android with volley , i try so many times but not success.


Solution

  • Try This

    try {
    RequestQueue requestQueue = Volley.newRequestQueue(this);
     try {
            JSONObject jsonObject=new JSONObject();
            JSONArray jsonArray=new JSONArray();
            JSONObject idJson=new JSONObject();
            idJson.put("id","12");
            jsonArray.put(jsonObject);
            jsonObject.put("data",jsonArray);
    
        } catch (JSONException e) {
            e.printStackTrace();
        }
    final String mRequestBody = jsonObject.toString();
    
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.i("LOG_RESPONSE", response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("LOG_RESPONSE", error.toString());
        }
    }) {
        @Override
        public String getBodyContentType() {
            return "application/json; charset=utf-8";
        }
    
        @Override
        public byte[] getBody() throws AuthFailureError {
            try {
                return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
            } catch (UnsupportedEncodingException uee) {
                VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
                return null;
            }
        }
    
    
    
        @Override
        protected Response<String> parseNetworkResponse(NetworkResponse response) {
            String responseString = "";
            if (response != null) {
                responseString = String.valueOf(response.statusCode);
            }
            return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
        }
    };
    
    requestQueue.add(stringRequest);
    } catch (JSONException e) {
    e.printStackTrace();
    }