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!