Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volley POST parameter not working?

Following is my Volley POST method Call. The POST parameters does not pass the data to web server/API tried almost everything like 1. Override byte[] getBody() 2. Parameter Type 3. Using JsonRequest

Parsing four POST parameters to the server and based on this going have response. What is missing in this code??

private void generateTicket(final String field1, final String field2 , final String field3, final String field4) {

    StringRequest strReq = new StringRequest(Method.POST,
                    AppConfig.URL_GENERATION, new Response.Listener<String>() {

                        @Override
                        public void onResponse(String response) {
                            try {
                                JSONObject jObj = new JSONObject(response);
                                boolean error = jObj.getBoolean("error");

                                // Check for error node in json
                                if (!error) {
                                // SUCCESS CODE GOES HEAR

                                } else {
                                    // Error in login. Get the error message
                                    String errorMsg = jObj.getString("msg");
                                    Toast.makeText(getApplicationContext(),
                                            errorMsg, Toast.LENGTH_LONG).show();
                                }
                            } catch (JSONException e) {
                                // JSON error
                                e.printStackTrace();
                            }

                        }
                    }, new Response.ErrorListener() {

                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.e(TAG, "API Error: " + error.getMessage());
                            Toast.makeText(getApplicationContext(),
                                    error.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    }) {

                @Override
                protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {  
                    // Posting parameters to the url
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("tag", "generate");
                    params.put("field1", field1);
                    params.put("field2", field2);
                    params.put("field3", field3);
                    params.put("field4", field4);           
                    return params;
                }

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String,String> params = new HashMap<String, String>();
                    params.put("Content-Type","application/x-www-form-urlencoded");
    //              params.put("Content-Type", "application/json");
                    return params;
                }

            };
            // Adding request to request queue
            AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }
like image 323
Amit Modak Avatar asked Jan 02 '26 02:01

Amit Modak


1 Answers

Please try request.setShouldCache(false); before add the request to the queue. I guess maybe it'll help.

By default, volley will cache the response. When it meets the url again, it will get from its cache not from the web. Even after reinstall app (like you do in debug), the cache is still here. But uninstall and install will clear the cache.

If you use the POST method, don't use the JsonRequest. It simply doesn't call getParams().

like image 108
Kay Wu Avatar answered Jan 03 '26 16:01

Kay Wu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!