OTP验证码android错误

时间:2016-10-19 10:21:53

标签: android android-volley one-time-password

我正在创建一个执行OTP验证方法的应用程序。 我按照此链接http://www.androidhive.info/2015/08/android-adding-sms-verification-like-whatsapp-part-2/

中的说明实现了代码

但是当我点击app app中的NEXT按钮时强行关闭

我得到错误的函数是:

private void requestForSMS(final String name, final String email, final String mobile) {
        StringRequest strReq = new StringRequest(Request.Method.POST,
                Config.URL_REQUEST_SMS, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, response.toString());

                try {
                    JSONObject responseObj = new JSONObject(response);

                    // Parsing json object response
                    // response will be a json object
                    boolean error = responseObj.getBoolean("error");
                    String message = responseObj.getString("message");

                    // checking for error, if not error SMS is initiated
                    // device should receive it shortly
                    if (!error) {
                        // boolean flag saying device is waiting for sms
                        pref.setIsWaitingForSms(true);

                        // moving the screen to next pager item i.e otp screen
                        viewPager.setCurrentItem(1);
                        txtEditMobile.setText(pref.getMobileNumber());
                        layoutEditMobile.setVisibility(View.VISIBLE);

                        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();

                    } else {
                        Toast.makeText(getApplicationContext(),
                                "Error: " + message,
                                Toast.LENGTH_LONG).show();
                    }

                    // hiding the progress bar
                    progressBar.setVisibility(View.GONE);

                } catch (JSONException e) {
                    //Toast.makeText(getApplicationContext(),"Error: " + e.getMessage(),Toast.LENGTH_LONG).show();

                    progressBar.setVisibility(View.GONE);
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                //Log.e(TAG, "Error: " + error.getMessage());
                //Toast.makeText(getApplicationContext(),error.getMessage(), Toast.LENGTH_SHORT).show();
                progressBar.setVisibility(View.GONE);
            }
        }) {

            /**
             * Passing user parameters to our server
             * @return
             */
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("name", name);
                params.put("email", email);
                params.put("mobile", mobile);

                //Log.e(TAG, "Posting params: " + params.toString());

                return params;
            }

        };

        // Adding request to request queue
        MyApplication.getInstance().addToRequestQueue(strReq);
    }

记录是:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              java.lang.NullPointerException
                                                                                  at com.my.application.activity.SmsActivity.requestForSMS(SmsActivity.java:245)
                                                                                  at com.my.application.activity.SmsActivity.validateForm(SmsActivity.java:157)
                                                                                  at com.my.application.activity.SmsActivity.onClick(SmsActivity.java:117)
                                                                                  at android.view.View.performClick(View.java:4211)
                                                                                  at android.view.View$PerformClick.run(View.java:17446)
                                                                                  at android.os.Handler.handleCallback(Handler.java:725)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                                  at android.os.Looper.loop(Looper.java:153)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5336)
                                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
                                                                                  at dalvik.system.NativeStart.main(Native Method)

使用Volley库.. 任何帮助?

1 个答案:

答案 0 :(得分:0)

 public void confirmOTP() throws JSONException {
        LayoutInflater lf = LayoutInflater.from(SignUp.this);
        View view = lf.inflate(R.layout.confirm_dialog, null);
        confirmButton = (Button) view.findViewById(R.id.buttonConfirm);
        inputOTP = (EditText) view.findViewById(R.id.editTextOtp);
        AlertDialog.Builder dialog = new AlertDialog.Builder(SignUp.this);
        dialog.setView(view);
        final AlertDialog alertDialog = dialog.create();
        alertDialog.show();
        alertDialog.setCanceledOnTouchOutside(false);
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                alertDialog.dismiss();
                final ProgressDialog load = ProgressDialog.show(SignUp.this, "Authenticating", "Please Wait ...", false, false);
                load.setCanceledOnTouchOutside(false);
                final String otp = inputOTP.getText().toString().trim();
                StringRequest stringRequest = new StringRequest(Request.Method.POST, ConfingDetalsActivity.CONFIRM_URL, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("Confirm Response Check>",response.toString());
                            String mesage;
                        try {
                            JSONObject jsonObject=new JSONObject(response);
                             mesage=jsonObject.getString("msg");

                       if(mesage.equals("success")){
                            Toast.makeText(SignUp.this, "Registerd sucessfully", Toast.LENGTH_LONG).show();

                            load.dismiss();
                            Intent in = new Intent(SignUp.this, SignupSucess.class);
                            startActivity(in);
                        } else {
                           load.dismiss();
                           Toast.makeText(SignUp.this, "Wrong OTP Please try again", Toast.LENGTH_SHORT).show();

                           confirmOTP();
                       }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        load.dismiss();
                        Toast.makeText(SignUp.this, error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
                ) {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {

                        Map params = new HashMap();
                        //Adding the parameters otp and username
                        params.put(ConfingDetalsActivity.KEY_OTP, otp);
                        if (businessFrag) {


                        } else {

                            params.put(ConfingDetalsActivity.CONSUMER_EMAIL, constant.EMAIl);
                        }

                        return params;
                    }
                };
                requestQueue.add(stringRequest);
            }
        });
    }

    public void register() {

        Log.d("RegisterCalled>>>>>>>>>", "register");
        final ProgressDialog load = ProgressDialog.show(SignUp.this, "Loading", "Pleas wait...", false, false);
        load.setCanceledOnTouchOutside(false);

        StringRequest request = new StringRequest(Request.Method.POST, ConfingDetalsActivity.REGISTER_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("Response Check>>>",response.toString());
                load.dismiss();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String message=jsonObject.getString("msg");
                    if (message.equals("success")) {
                        confirmOTP();
                    } else {
                        Toast.makeText(SignUp.this, "User Name or phone already register", Toast.LENGTH_SHORT).show();

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                load.dismiss();
                Toast.makeText(SignUp.this, error.getMessage(), Toast.LENGTH_SHORT).show();

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> parms = new HashMap<String,String>();
                if (businessFrag) {
                    parms.put(ConfingDetalsActivity.BUSINESS_ZIP_CODE, constant.B_ZIP_CODE);
                    parms.put(ConfingDetalsActivity.BUSINESS_INDUSTRY, constant.B_INDUSTRY);
                    parms.put(ConfingDetalsActivity.BUSINESS_PHONE, constant.B_MOBILE);
                    parms.put(ConfingDetalsActivity.BUSINESS_EMAIL, constant.B_EMAIl);
                    parms.put(ConfingDetalsActivity.BUSINESS_PASSWORD, constant.B_PASSSOWRD);
                    parms.put(ConfingDetalsActivity.SIGNUP_TYPE,"1");

                } else {
                    parms.put(ConfingDetalsActivity.CONSUMER_FNAME, constant.F_NAME);
                    parms.put(ConfingDetalsActivity.CONSUMER_LNAME, constant.L_NAME);
                    parms.put(ConfingDetalsActivity.CONSUMER_PHONE, constant.MOBILE);
           constant.EMAIl);
                    parms.put(ConfingDetalsActivity.CONSUMER_PASSWORD, constant.PASSSOWRD);
                    parms.put(ConfingDetalsActivity.SIGNUP_TYPE,"0");
                }
                return parms;
            }
        };
        requestQueue.add(request);
    }