当前位置: 首页 > 工具软件 > Mobile-OTP > 使用案例 >

android otp接口,android - How to validate otp from api - Stack Overflow

史涵育
2023-12-01

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 {

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

 类似资料: