-1

I'm making OTP based registrations how do I validate my OTP number please help. here are 2 scripts 1 is a validation activity 2 is an async task running ok http req 3 there is one more activity which takes a user phone number and sends OTP SMS which is working fine

I just wanna check if OTP on my edit text matches with message user received

package com.example.test2;

import android.content.Intent;
import android.os.AsyncTask;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

class Verifyotp extends AsyncTask<String, Void, Void> {

    public static String string;
    @Override
    protected Void doInBackground(String... strings) {

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("mobile_no", MainActivity.phoneNo)
                .addFormDataPart("otp", OTP_activity.OTP)
                .build();
        Request request = new Request.Builder()
                .url("http://127.0.0.1:8000/api/verifyotp")
                .method("POST", body)
                .addHeader("Accept", "application/json")
                .build();
        try {
            Response response = client.newCall(request).execute();
            string = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}
package com.example.test2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Objects;

public class OTP_activity extends AppCompatActivity {

    public static String OTP;
    TextView mssg;
    EditText ED;
    Button ver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_otp_activity);

        mssg = (TextView) findViewById(R.id.txtmssg);
        ver = (Button) findViewById(R.id.verify);
        ED = (EditText)findViewById(R.id.otp);


        mssg.setText("We have sent you an SMS on " + MainActivity.phoneNo + " with 4 digit verification code.");
        ver.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OTP = ED.getText().toString();
                  Verifyotp chk= new Verifyotp();
                  chk.execute();
                 int i = Verifyotp.string.length();
              if (i < 20){

                  Toast.makeText(getApplicationContext(),
                          "please try again.", Toast.LENGTH_LONG).show();
              }
              else {
                  Intent in = new Intent(OTP_activity.this, Home_Acivity.class);
                  startActivity(in);
              }

            }
        });

    }

}
Any help is appreciated
Narendranath Reddy
  • 3,833
  • 3
  • 13
  • 32
  • So, what happens right now? Is it throwing an error? You seem to check the length of the response received from the server. You don't want to check what the server response is? I would suggest checking the response message from the server and display the error or success message based on that. – Akhilesh B Chandran Mar 01 '20 at 07:35
  • I'm trying to do that only but I don't know how I defined string on resources {"result":false} whick is response for failure and then comparing it with response I'm getting but it seems that I'm doing something wrong – Dhairya Bhatt Mar 01 '20 at 08:08
  • If your server response is like this: `{"result":false}` , I assume your server is returning the response as JSON. What you have to do is, parse that JSON and can then compare the `result` to see if its true or false, and display the message based on it. Have a look at this for the parsing example: https://stackoverflow.com/questions/8091051/how-to-parse-json-string-in-android/8091111 – Akhilesh B Chandran Mar 01 '20 at 08:22
  • Response response = client.newCall(request).execute(); string = response.body().string(); JSONObject jsonObject = new JSONObject(string); String string2 = jsonObject.getString("result"); then i guess this should Work? – Dhairya Bhatt Mar 01 '20 at 08:31

1 Answers1

0

You Could Use Firebase OTP Authentication For This Work.

Tushar
  • 54
  • 4
  • It's not "you should", instead "you could also". Because you can even implement third party SMS API to send the OTP and do the validation, just like what the OP is trying is to do. I think it's not mandatory to use Firebase for this purpose! :) – Akhilesh B Chandran Mar 01 '20 at 07:40
  • I just need to know that how do I validate the OTP that I'm receiving from API with OTP IM Entering in validation activity. I'm receiving different Response from okhttp of first script if validation failed it'll response {"result":false} if validation is successful it'll register user in that json response now I need to know that how do I validate user based on this okhttp response I'm getting – Dhairya Bhatt Mar 01 '20 at 07:52