Send POST request with JSON data using Volley

前端 未结 8 722
梦谈多话
梦谈多话 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:54

    final String URL = "/volley/resource/12";
    // Post params to be sent to the server
    HashMap params = new HashMap();
    params.put("token", "AbCdEfGh123456");
    
    JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
           new Response.Listener() {
               @Override
               public void onResponse(JSONObject response) {
                   try {
                       VolleyLog.v("Response:%n %s", response.toString(4));
                   } catch (JSONException e) {
                       e.printStackTrace();
                   }
               }
           }, new Response.ErrorListener() {
               @Override
               public void onErrorResponse(VolleyError error) {
                   VolleyLog.e("Error: ", error.getMessage());
               }
           });
    
    // add the request object to the queue to be executed
    ApplicationController.getInstance().addToRequestQueue(req);
    

    refer

提交回复
热议问题