Reputation: 35
I am requesting Json data with post method In Postman it is working but With volley it is giving 422 Error . what is the issue i am not getting URL http://13.232.142.23:3000/api/register with post method Json data as
{ "phone":"7567878789", "email":"[email protected]", "password":"1234", "fullname":"asdw", "device_id":"dsvvssvsd", "otp":"1234" }
private void registerUser(final String phone,final String otp,final String fullname,final String email, final String password, final String device_id )
{
Log.e(TAG, "otp12 " +otp11);
String tag_string_req = "req_register";
Map<String, String> params = new HashMap<String, String>();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
AppConfig.Base_Url+AppConfig.App_api+AppConfig.URL_REGISTER, new JSONObject(params),
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response1) {
Log.d(TAG, "Register Response: " + response1.toString());
try {
JSONObject jObj = new JSONObject(String.valueOf(response1));
String response = jObj.getString("response");
String status =jObj.getString("status");
if (status!=null && status.equals("success")) {
launchAgeScreen();
Log.e(TAG, "123" + fullname);
Log.e(TAG, "status: " + status);
Log.e(TAG, "paswword: " + password);
Log.e(TAG, "response2163123: " + response);
}else if (status!=null && status.equals("failed") && response.equals("Duplicate_Phone_No")){
AlertDialog.Builder builder =new AlertDialog.Builder(RegisterActivity.this);
builder.setTitle("Registration Error");
builder.setMessage("You have already registered with this number. Please click Okay to Login");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(RegisterActivity.this,LoginActivityWithoutSharedPreference.class);
startActivity(intent);
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}else if (status!=null && status.equals("failed") && response.equals("Duplicate_Mail_ID")){
AlertDialog alertDialog = new AlertDialog.Builder(RegisterActivity.this, R.style.MyDialogTheme).create();
// Setting Dialog Title
alertDialog.setTitle("Registration Error");
// Setting Dialog Message
alertDialog.setMessage("You have already registered with this Email. Please click Okay to Login");
alertDialog.setCanceledOnTouchOutside(false);
// Setting OK Button
alertDialog.setButton("Okay", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
launchAgeScreen();
}
});
// Showing Alert Message
alertDialog.show();
}
} catch (JSONException e) {
e.printStackTrace();
//Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
AlertDialog alertDialog = new AlertDialog.Builder(RegisterActivity.this, R.style.MyDialogTheme).create();
// Setting Dialog Title
alertDialog.setTitle("Network/Connection Error");
// Setting Dialog Message
alertDialog.setMessage("Internet Connection is poor OR The Server is taking too long to respond.Please try again later.Thank you.");
// Setting Icon to Dialog
// alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("Okay", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
// Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
// Log.e(TAG, "Registration Error: " + error.getMessage());
/*Toast.makeText(context,
context.getString(R.string.error_network_timeout),
Toast.LENGTH_LONG).show();*/
} else if (error instanceof AuthFailureError) {
//TODO
} else if (error instanceof ServerError) {
//TODO
} else if (error instanceof NetworkError) {
//TODO
} else if (error instanceof ParseError) {
//TODO
}
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError{
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
params.put("phone",phone);
params.put("otp",otp);
params.put("fullname", fullname);
params.put("email",email);
params.put("password",password);
params.put("device_id", device_id);
return params;
}
};
Upvotes: 1
Views: 1309
Reputation: 76649
422 Unprocessable Entity
...so you are creating a HashMap
:
Map<String, String> params = new HashMap<String, String>();
and then post that empty HashMap
, without having put anything inside it:
new JSONObject(params)
but in other to actually post data, that HashMap
needs to be populated:
Map<String, String> params = new HashMap<String, String>();
params.put("phone", phone);
params.put("email", email);
params.put("password", password);
params.put("fullname", fullname);
params.put("device_id", device_id);
params.put("otp", otp);
Upvotes: 1