Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Volley POST Sending Parameters is always null

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 when i send data into server using volley.here i attached the code please check it.Here i am using fragments.

Code Section

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());
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
            url, null,
            new Response.Listener<JSONObject>() {

        @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();
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("name", "Ajay K K");
            params.put("mailid", "[email protected]");
            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");

            return params;
        }

    };

    // Adding request to request queue
    rq.add(jsonObjReq);
like image 211
user3871606 Avatar asked Mar 18 '23 23:03

user3871606


1 Answers

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<JSONObject> 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", "[email protected]");
    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<JSONObject>() {

    @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);
like image 62
Cinakyn Avatar answered Mar 29 '23 22:03

Cinakyn