Send POST request with JSON data using Volley

前端 未结 8 619
梦谈多话
梦谈多话 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<String, String> params = new HashMap<String, String>();
    params.put("token", "AbCdEfGh123456");
    
    JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
           new Response.Listener<JSONObject>() {
               @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

    0 讨论(0)
  • 2020-11-22 12:57
    final Map<String,String> params = new HashMap<String,String>();
            params.put("email", customer.getEmail());
            params.put("password", customer.getPassword());
            String url = Constants.BASE_URL+"login";
    
    doWebRequestPost(url, params);
    
    
    public void doWebRequestPost(String url, final Map<String,String> json){
            getmDialogListener().showDialog();
    
        StringRequest post = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    getmDialogListener().dismissDialog();
                    response....
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d(App.TAG,error.toString());
                getmDialogListener().dismissDialog();
    
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = json;
    
                return map;
            }
        };
        App.getInstance().getRequestQueue().add(post);
    
    }
    
    0 讨论(0)
提交回复
热议问题