Send POST request with JSON data using Volley

前端 未结 8 735
梦谈多话
梦谈多话 2020-11-22 12:07

I would like to send a new JsonObjectRequest request:

  • I want to receive JSON data (response from server): OK
  • I want to send JSON form

8条回答
  •  广开言路
    2020-11-22 12:42

    You can also send data by overriding getBody() method of JsonObjectRequest class. As shown below.

        @Override
        public byte[] getBody()
        {
    
            JSONObject jsonObject = new JSONObject();
            String body = null;
            try
            {
                jsonObject.put("username", "user123");
                jsonObject.put("password", "Pass123");
    
                body = jsonObject.toString();
            } catch (JSONException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            try
            {
                return body.toString().getBytes("utf-8");
            } catch (UnsupportedEncodingException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
    

提交回复
热议问题