1

I am using the GenericRequest (an extension of the built-in jsonrequest) to make a REST call to a server that takes in a json object and returns a string, which is "0" if the json object already exists and a nonzero string otherwise.

However, with the following code, I always get a "0" back no matter what I sent.

    JSONObject userobj = new JSONObject();
    try {
        userobj.put("email",email);
        userobj.put("password",password);
        userobj.put("username",name);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    Log.d(TAG, userobj.toString());

    GenericRequest jsonObjReq = new GenericRequest(Request.Method.POST, REGISTER_URL, String.class, userobj,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    // Handle access token.
                    Log.d(TAG, "Register received: " + response);
                    long token = Long.parseLong(response);
                    if(token == 0) {
                        Log.d(TAG, "Received 0!");
                        Toast.makeText(MainActivity.this, R.string.registerfail_toast, Toast.LENGTH_LONG).show();
                    } else {
                        Log.d(TAG, "Register success!");
                        Toast.makeText(MainActivity.this, R.string.Welcome, Toast.LENGTH_LONG).show();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d(TAG, error.toString());
                    Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                }
            }) {

        @Override
        public String getBodyContentType() {
            return "application/json";
        }

    };

    jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(0, -1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    helper.add(jsonObjReq);

When testing in Postman, given the input like: { "email": "dlee23122", "password": "1234", "username": "dlee23122" }, it gives back a nonzero string. (Screenshot as follows.) But when given a slightly different input using the Volley, it keeps giving back a "0". What could be the reason?

Thanks in advance!

enter image description here

Community
  • 1
  • 1
DonRal
  • 13
  • 4
  • take a screenshot of your postman http heards or add them here. postman is not a true REST client. – Vahid Hashemi Apr 05 '17 at 15:09
  • @Vahidhashemi Hi, thanks for helping. The screenshot of Postman has been added. The header is "Content-Type":"application/json". – DonRal Apr 05 '17 at 15:17
  • Turns out this is a problem of what mentioned in [Here](http://stackoverflow.com/questions/26022133/how-can-i-delete-the-namevaluepairs-key-from-the-jsonobject). – DonRal Apr 05 '17 at 19:47

1 Answers1

0

Even i faced the same problem, volley default connection timeout is set to 5 sec and it was posting two times, so check for time in postman on right side, if it is closer to 5000ms or greater this might be the problem. My problem got solved by adding the following to Volley request:

DefaultRetryPolicy retryPolicy = new DefaultRetryPolicy(0, -1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
   jsonObjectRequest.setRetryPolicy(retryPolicy);

if you want set custom retry policy look at this post Change Volley timeout duration

Basha K
  • 1,509
  • 1
  • 11
  • 16