how to send json object to server using volley in android

后端 未结 2 1271
小蘑菇
小蘑菇 2020-11-27 19:24

I want to send the JSONObject to server using POST method . I have used volley library to pass the string params its working fine, but if i try to use json ob

2条回答
  •  执笔经年
    2020-11-27 19:55

    Third parameter in JsonObjectRequest is for passing post parameters in jsonobject form. And for header you need to send two separate values one for content-type one for charset.

      RequestQueue queue = Volley.newRequestQueue(this);
    
      private void makeJsonObjReq() {
        showProgressDialog();
    
    
                Map postParam= new HashMap();
                postParam.put("un", "xyz@gmail.com");
                postParam.put("p", "somepasswordhere");
    
    
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
                Const.URL_LOGIN, new JSONObject(postParam),
                new Response.Listener() {
    
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d(TAG, response.toString());
                        msgResponse.setText(response.toString());
                        hideProgressDialog();
                    }
                }, new Response.ErrorListener() {
    
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                        hideProgressDialog();
                    }
                }) {
    
            /**
             * Passing some request headers
             * */
            @Override
            public Map getHeaders() throws AuthFailureError {
                HashMap headers = new HashMap();
                headers.put("Content-Type", "application/json; charset=utf-8");
                return headers;
            }
    
    
    
        };
    
        jsonObjReq.setTag(TAG);
        // Adding request to request queue
        queue.add(jsonObjReq);
    
        // Cancelling request
        /* if (queue!= null) {
        queue.cancelAll(TAG);
        } */
    
    }
    

提交回复
热议问题