【问题标题】:I'm having trouble with phone number authentication. Verify_otp generates a unique code for any verified number我在电话号码验证方面遇到问题。 Verify_otp 为任何经过验证的号码生成唯一代码
【发布时间】:2019-04-01 03:08:45
【问题描述】:

我在进行电话号码验证时遇到问题。 Verify_otp 为任何经过验证的号码生成一个唯一代码。但最大的问题是它只生成以 6599 开头的数字 ..... 数字 6598 .... 它不再生成代码。

我找不到让他接受任何电话号码的渠道。

这是 RegistrationModel.java 文件的一部分

public static class OTP_Details{


        /**
         * status : 2
         * message : Otp Sent to phone for Verification
         * otp : 2017
         * auto_otp : 1
         */

        private int status;
        private String message;
        private String otp;
        private int auto_otp;

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public String getOtp() {
            return otp;
        }

        public void setOtp(String otp) {
            this.otp = otp;
        }

        public int getAuto_otp() {
            return auto_otp;
        }

        public void setAuto_otp(int auto_otp) {
            this.auto_otp = auto_otp;
        }
    }
}

【问题讨论】:

  • 什么是verify_otp?它不会出现在您向我们展示的代码中的任何地方。您使用什么服务来验证电话号码,在哪里进行验证?
  • Java与JS无关,IDE与平台/语言无关
  • 准备好了!我已在我列出的文件下方提交。键入手机并单击发送 otp 后,返回的代码始终是代码 2017,这是我发送的第一个代码,但如果数字开头是这样的,它只会发送此代码 2017:6599 .... 如果数字例如:6598 ....不生成代码2017,无法继续注册。

标签: android one-time-password


【解决方案1】:

这是 Vetify_otp

package com.motofacil.passageiro;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.motofacil.passageiro.accounts.RegistrationModel;
import com.motofacil.passageiro.accounts.ResultCheckMessage;
import com.motofacil.passageiro.accounts.ResultChecker;
import com.motofacil.passageiro.manager.ApiManager;
import com.motofacil.passageiro.manager.SessionManager;
import com.motofacil.passageiro.samwork.Config;
import com.motofacil.passageiro.urls.Apis;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hbb20.CountryCodePicker;

import java.util.HashMap;


public class Verify_OTP extends AppCompatActivity implements ApiManager.APIFETCHER {

    ApiManager apiManager ;
    GsonBuilder gsonBuilder;
    RegistrationModel.OTP_Details otp_details;
    private TextView otpError_txt;
    private EditText otp_input, edt_enter_phone;
    private LinearLayout submit_otp_layout;
    SessionManager sessionManager ;
    Gson gson;
    String code, input_phone_number, otp;
    CountryCodePicker codePicker;
    private static final int KEY_REGISTER = 110;
    Button generate_otp;
    LinearLayout submit_otp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gsonBuilder = new GsonBuilder();
        gson = gsonBuilder.create();
        apiManager = new ApiManager(this , this , this  );
        sessionManager = new SessionManager(Verify_OTP.this);
        otp_details = new RegistrationModel.OTP_Details();
        setContentView(R.layout.activity_verify__otp);

        gsonBuilder = new GsonBuilder();
        gson = gsonBuilder.create();

        submit_otp = (LinearLayout) findViewById(R.id.otp_submit);
        generate_otp = (Button) findViewById(R.id.generate_otp);
        edt_enter_phone = (EditText)findViewById(R.id.edt_enter_phone);
        otp_input = (EditText)findViewById(R.id.otp_edt);
        otpError_txt = (TextView)findViewById(R.id.otp_verifier_txt);
        submit_otp_layout = (LinearLayout)findViewById(R.id.otp_submit);
        codePicker = (CountryCodePicker)findViewById(R.id.otp_ccp);

        submit_otp.setEnabled(false);

        generate_otp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                input_phone_number = edt_enter_phone.getText().toString().trim();
                Log.e("input_phone_number====", input_phone_number);
                code = codePicker.getSelectedCountryCodeWithPlus();
                Log.e("COUNTRY_CODE_PICKER===", code);
                if (input_phone_number.equals("")){
                    Toast.makeText(Verify_OTP.this, R.string.required_field_missing, Toast.LENGTH_SHORT).show();
                }else {
                    getOTP(code+input_phone_number);

                }
            }
        });

        submit_otp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (otp_input.getText().toString().equals("")) {
                    Toast.makeText(Verify_OTP.this, R.string.required_field_missing, Toast.LENGTH_SHORT).show();
                } else if (!otp_input.getText().toString().equals(otp)) {
                   // Toast.makeText(Verify_OTP.this, R.string.invalid_otp, Toast.LENGTH_SHORT).show();

                    Intent intent = new Intent();
                    intent.putExtra("phone_number", code + input_phone_number);
                    setResult(Activity.RESULT_OK, intent);
                     finish();
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("phone_number", code + input_phone_number);
                    setResult(Activity.RESULT_OK, intent);
                    finish();


                }
            }
            });
    }


    private void getOTP(String phone) {
        HashMap<String , String > bodyparameters  = new HashMap<String, String>();
        bodyparameters.put("phone" , phone);
        bodyparameters.put("flag" ,"1");
        apiManager.execution_method_post(Config.ApiKeys.KEY_VERIFY_OTP ,""+  Apis.SEND_OTP, bodyparameters, true,ApiManager.ACTION_SHOW_TOP_BAR);
    }




    @Override
    public void onFetchComplete(Object script, String APINAME) {

        if(APINAME.equals(""+Config.ApiKeys.KEY_VERIFY_OTP)){
            submit_otp.setEnabled(true);
            RegistrationModel.OTP_Details otp_response = gson.fromJson("" + script, RegistrationModel.OTP_Details.class);
            //  finilalizeActivity();
            Log.e("**OTP_SCRIPT-----", String.valueOf(otp_response.getMessage() + otp_response.getOtp() + otp_response.getStatus()));
            otp = otp_response.getOtp();

            Log.d("otp==normal sign up==", otp);
            otp_input.setText(""+otp);
            otp_input.requestFocus();
            if(otp_response.getAuto_otp() == 1){
                otp_input.setText(""+otp);
            }

        }else {
            try{ResultChecker rcheck = gson.fromJson("" + script, ResultChecker.class);
                Log.e("**OTP_SCRIPT-----", String.valueOf(script));

                if(rcheck.getResult() == 1){
                    Log.e("**RCHHECKK---", String.valueOf(rcheck.getResult()));
                    RegistrationModel.OTP_Details otp_response = gson.fromJson("" + script, RegistrationModel.OTP_Details.class);
                }else {
                    ResultCheckMessage rr = gson.fromJson("" + script, ResultCheckMessage.class);
                    Toast.makeText(this, ""+rr.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }catch (Exception e){}
        }



    }


    @Override
    public void onFetchResultZero(String s) {

    }

}

【讨论】:

    【解决方案2】:

    这是 Activity_forgotpass_verify_otp.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.motofacil.passageiro.ForgotPass_Verify_OTP">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@color/pure_white"
            android:orientation="horizontal">
    
            <LinearLayout
                android:id="@+id/otp_back"
                android:layout_width="50dp"
                android:layout_height="match_parent"
                android:gravity="center"
                android:visibility="invisible">
    
                <ImageView
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:src="@drawable/ic_left_sort"
                    android:tint="@color/icons_8_muted_grey"/>
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center">
    
                <com.motofacil.passageiro.accounts.TypefaceDosisRegular
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/verifyOTP"
                    android:textColor="@color/icons_8_muted_grey"
                    android:textSize="16dp" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="50dp"
                android:layout_height="match_parent" />
        </LinearLayout>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/icons_8_muted_grey" />
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@drawable/login_banner"></ImageView>
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#bf000000"></RelativeLayout>
    
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">
    
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/icons_8_muted_grey" />
    
                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1">
    
    
    
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center|top"
                        android:layout_marginTop="20dp"
                        android:gravity="center"
                        android:orientation="vertical">
    
                        <LinearLayout
                            android:id="@+id/phone_layout"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="25dp"
                            android:layout_marginLeft="15dp"
                            android:background="@drawable/shapes_white_transparent"
                            android:layout_marginRight="15dp"
                            android:gravity="center"
                            android:orientation="horizontal">
    
    
                            <com.hbb20.CountryCodePicker
                                android:id="@+id/otp_ccp"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                app:hideNameCode="true"
                                app:keyboardAutoPopOnSearch="false"
                                app:showFlag="false"
                                android:layout_marginLeft="5dp"
                                app:defaultCode="55"
                                app:textSize="15dp"/>
    
                            <EditText
                                android:id="@+id/edt_enter_phone"
                                android:layout_width="match_parent"
                                android:layout_height="40dp"
                                android:layout_marginLeft="10dp"
                                android:background="@android:color/transparent"
                                android:ems="10"
                                android:drawableTint="@color/icons_8_muted_grey"
                                android:gravity="center|left"
                                android:hint="Enter Phone Number"
                                android:inputType="phone"
                                android:maxLength="10"
                                android:minLines="1"
                                android:padding="5dp"
                                android:textColor="@color/pure_black"
                                android:textSize="17dp" />
    
                        </LinearLayout>
    
                        <!--
                                            <View
                                                android:layout_width="match_parent"
                                                android:layout_height="0.85dp"
                                                android:layout_marginBottom="10dp"
                                                android:layout_marginLeft="15dp"
                                                android:layout_marginRight="15dp"
                                                android:layout_marginTop="6dp"
                                                android:background="@color/icons_8_muted_yellow" />-->
                        <Button
                            android:id="@+id/generate_otp"
                            android:layout_marginTop="20dp"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/generateOTP" />
    
                        <TextView
                            android:id="@+id/otp_txt"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="35dp"
                            android:layout_marginLeft="15dp"
                            android:layout_marginRight="15dp"
                            android:text="@string/otp_text"
                            android:textColor="@color/pure_white"
                            android:textSize="17dp" />
    
    
    
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_marginBottom="70dp"
                            android:orientation="vertical">
    
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="40dp"
                                android:gravity="center"
                                android:layout_marginLeft="15dp"
                                android:layout_marginRight="15dp"
                                android:layout_marginTop="30dp"
                                android:background="@drawable/shapes_white_transparent"
                                android:orientation="horizontal">
    
    
    
    
                                <EditText
                                    android:id="@+id/otp_edt"
                                    android:layout_width="match_parent"
                                    android:layout_height="fill_parent"
                                    android:background="@android:color/transparent"
                                    android:drawableTint="@color/icons_8_muted_grey"
                                    android:gravity="center|left"
                                    android:hint="@string/enter_otp"
                                    android:inputType="number"
                                    android:paddingLeft="10dp"
                                    android:textColor="@color/pure_black"
                                    android:textSize="17dp" />
    
    
                                <TextView
                                    android:id="@+id/otp_verifier_txt"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_marginBottom="20dp"
                                    android:gravity="center|right"
                                    android:padding="10dp"
                                    android:text="Email not valid"
                                    android:textColor="@color/icons_8_muted_red"
                                    android:visibility="gone"/>
    
    
                            </LinearLayout>
    
    
                        </LinearLayout>
    
                    </LinearLayout>
                </ScrollView>
    
                <LinearLayout
                    android:id="@+id/otp_submit"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:background="@color/colorPrimary"
                    android:gravity="center">
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/submitOTP"
                        android:textColor="@color/pure_white"
                        android:textSize="16dp" />
                </LinearLayout>
    
            </LinearLayout>
        </FrameLayout>
    
    
    
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      准备好了!我已在我列出的文件下方提交。键入手机并单击发送 otp 后,返回的代码始终是代码 2017,即我发送的第一个代码,但如果数字开头是这样的,它只会发送此代码 2017:6599 .... 如果数字例如:6598 ....不生成代码2017,无法继续注册。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-27
        • 1970-01-01
        • 1970-01-01
        • 2011-09-21
        • 1970-01-01
        相关资源
        最近更新 更多