showing results for - "how to send json data to server in android using volley"
Meryem
18 Jun 2019
1  RequestQueue queue = Volley.newRequestQueue(this);
2
3  private void makeJsonObjReq() {
4    showProgressDialog();
5
6
7            Map<String, String> postParam= new HashMap<String, String>();
8            postParam.put("un", "xyz@gmail.com");
9            postParam.put("p", "somepasswordhere");
10
11
12    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
13            Const.URL_LOGIN, new JSONObject(postParam),
14            new Response.Listener<JSONObject>() {
15
16                @Override
17                public void onResponse(JSONObject response) {
18                    Log.d(TAG, response.toString());
19                    msgResponse.setText(response.toString());
20                    hideProgressDialog();
21                }
22            }, new Response.ErrorListener() {
23
24                @Override
25                public void onErrorResponse(VolleyError error) {
26                    VolleyLog.d(TAG, "Error: " + error.getMessage());
27                    hideProgressDialog();
28                }
29            }) {
30
31        /**
32         * Passing some request headers
33         * */
34        @Override
35        public Map<String, String> getHeaders() throws AuthFailureError {
36            HashMap<String, String> headers = new HashMap<String, String>();
37            headers.put("Content-Type", "application/json; charset=utf-8");
38            return headers;
39        }
40
41
42
43    };
44
45    jsonObjReq.setTag(TAG);
46    // Adding request to request queue
47    queue.add(jsonObjReq);
48
49    // Cancelling request
50    /* if (queue!= null) {
51    queue.cancelAll(TAG);
52    } */
53
54}
55