Android Volley POST Sending Parameters is always null

前端 未结 4 405
攒了一身酷
攒了一身酷 2021-01-20 01:49

I am new in android.Now i am doing one application.For this one i need to send data into server.Now i am using Volley post method.But the parameters is always shows null wh

4条回答
  •  面向向阳花
    2021-01-20 02:24

    Don't override getParams(). JsonObjectRequest uses third argument in constructor to get post parameters. below is documentation contained in volley's code

    /**
     * Creates a new request.
     * @param method the HTTP method to use
     * @param url URL to fetch the JSON from
     * @param jsonRequest A {@link JSONObject} to post with the request. Null is allowed and
     *   indicates no parameters will be posted along with request.
     * @param listener Listener to receive the JSON response
     * @param errorListener Error listener, or null to ignore errors.
     */
    public JsonObjectRequest(int method, String url, JSONObject jsonRequest,
            Listener listener, ErrorListener errorListener) {
        super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
                    errorListener);
    }
    

    uses like this.

    String url = "http://192.168.1.182:8084/name/registration.jsp";
    
    final ProgressDialog pDialog = new ProgressDialog(this.getActivity());
    pDialog.setMessage("Loading...");
    pDialog.show();    
    RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());
    
    JSONObject params = new JSONObject();
    try {
        params.put("name", "Ajay K K");
        params.put("mailid", "ajaykk50@gmail.com");
        params.put("phone", "8086327023");
        params.put("place", "Calicut");
        params.put("longitude","44444.3333");
        params.put("latitude","666666.3333");
        params.put("wheel", "1");
        params.put("type", "owner");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
            url, params, //Not null.
            new Response.Listener() {
    
        @Override
        public void onResponse(JSONObject response) {
            Log.d(TAG, response.toString());
            // pDialog.hide();
        }
    }, new Response.ErrorListener() {
    
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            //pDialog.hide();
        }
    });
    
    // Adding request to request queue
    rq.add(jsonObjReq);
    

提交回复
热议问题