BasicNetwork.performRequest:排球的意外的响应代码422

时间:2019-01-31 19:15:26

标签: android json android-volley

我正在使用post方法请求Json数据。在Postman中,它正在工作,但是在凌空下,它给出了422 Error。我没有得到什么问题 网址http://13.232.142.23:3000/api/register 邮寄方式 Json数据为

{ “ phone”:“ 7567878789”, “电子邮件”:“ n@g.com”, “密码”:“ 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;
            }

        };

1 个答案:

答案 0 :(得分:1)

422 Unprocessable Entity ...所以您要创建一个HashMap

Map<String, String> params = new HashMap<String, String>();

,然后在没有放入任何内容的情况下发布空的HashMap

new JSONObject(params)

但要实际发布数据,还需要填充HashMap

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);