Android请求服务器获取短信验证码实现注册功能

时间:2022-11-26 14:40:31

今天来实现一下请求服务器获取短信验证码实现注册功能
由于用的是别人的服务器接口代码中我会把接口地址删掉,谅解。
请求服务器获取验证码需要传入相应的字段,如下:

/**
* 获取手机验证码 * @method get
* @param string phone 手机号码
* @param string type 1-注册 2-找回密码 3-账号验证
* @param string token type=3账号验证时必传
*/

注册也和这一样,如下:

  /**
* 用户注册
* * @method post
* @param string phone 手机号码(必填)
* @param string captcha 验证码(必填)
* @param string password 密码(必填)
* @param string repassword 重复密码(必填)
*/

新建一个项目名为GetSmsCode然后在build.gradle中添加网络请求框架Xutils3.5.0的依赖请求服务器时会用到

compile 'org.xutils:xutils:3.5.0'

然后在AndroidManifest.xml中添加相应的权限

 <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

权限配置完成后需要新建一个Application进行Xutils3.5.0的初始化,再到AndroidManifest中指定即可。

package com.shiran.getsmscode;

import android.app.Application;

import org.xutils.x;

/**
* Created by Administrator on 2017/12/19.
*/


public class MyApplication extends Application{

@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this); //初始化
}
}

接下来是我们代码部分了
activity_main.xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#F1F1F1">


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dip"
android:background="#FFFFFF">


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="注册"
android:textSize="18sp"/>

</RelativeLayout>

<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#FFFFFF"/>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="10dp"
android:addStatesFromChildren="true"
android:background="@drawable/bg_et_selector"
android:orientation="horizontal" >


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:text="手机号:"
android:textSize="16sp" />


<EditText
android:id="@+id/register_et_phone"
android:gravity="center_vertical"
android:background="@null"
android:layout_width="0.0dip"
android:layout_height="match_parent"
android:inputType="phone"
android:layout_weight="1.0"
android:digits="0123456789"
android:textSize="15sp"
android:hint="请输入手机号" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:addStatesFromChildren="true"
android:background="@drawable/bg_et_selector"
android:orientation="horizontal" >


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:text="验证码:"
android:textSize="16sp" />


<EditText
android:id="@+id/register_et_code"
android:gravity="center_vertical"
android:background="@null"
android:layout_width="0.0dip"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:textSize="15sp"
android:hint="输入验证码" />


<TextView
android:id="@+id/register_text_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_gravity="center"
android:text="获取验证码"
android:textColor="#FFFFFF"
android:textSize="12sp"
android:background="@drawable/shape_btn_blue"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:addStatesFromChildren="true"
android:background="@drawable/bg_et_selector"
android:orientation="horizontal" >


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:text="密 码:"
android:textSize="16sp" />


<EditText
android:id="@+id/register_et_password"
android:gravity="center_vertical"
android:background="@null"
android:layout_width="0.0dip"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:textSize="15sp"
android:password="true"
android:digits="0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
android:hint="请输入密码" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="10dp"
android:addStatesFromChildren="true"
android:background="@drawable/bg_et_selector"
android:orientation="horizontal" >


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:text="重 复:"
android:textSize="16sp" />


<EditText
android:id="@+id/register_et_repassword"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:layout_width="0.0dp"
android:gravity="center_vertical"
android:background="@null"
android:textSize="15sp"
android:password="true"
android:digits="0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
android:hint="再次输入密码" />

</LinearLayout>

<Button
android:id="@+id/register_btn_submit"
android:layout_width="match_parent"
android:layout_marginTop="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_height="45dp"
android:background="@drawable/shape_btn_blue"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:text="提交" />


</LinearLayout>

用到的资源文件
bg_et_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_focused="true">

<shape android:shape="rectangle">

<gradient android:angle="270.0"
android:endColor="#FFFFFF"
android:startColor="#FFFFFF" />


<stroke android:width="1dp"
android:color="#44B4F3" />


<corners android:bottomLeftRadius="5.0dip"
android:bottomRightRadius="5.0dip"
android:topLeftRadius="5.0dip"
android:topRightRadius="5.0dip" />

</shape>
</item>

<item>
<shape android:shape="rectangle">

<gradient android:angle="270.0"
android:endColor="#FFFFFF"
android:startColor="#FFFFFF" />


<stroke android:width="1dp"
android:color="#FFFFFF" />


<corners
android:bottomLeftRadius="5.0dip"
android:bottomRightRadius="5.0dip"
android:topLeftRadius="5.0dip"
android:topRightRadius="5.0dip" />

</shape>
</item>
</selector>

shape_btn_blue.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">


<solid android:color="#44B4F3"/>

<corners android:radius="8.0px"/>

<padding
android:top="10dp"
android:bottom="10dp"
android:left="10dp"
android:right="10dp"/>


</shape>

MainActivity中的代码如下:

package com.shiran.getsmscode;

import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private static final String TAG = "MainActivity";
private EditText mEtPhone = null;
private EditText mEtCode = null;
private TextView mTextCode = null;
private EditText mEtPassWord = null;
private EditText mEtRePassWord = null;
private Button mBtnSubmit = null;
private int type = 1;
private CountDownTimer mCountDownTimer = new CountDownTimer(60000, 1000) {
@Override
public void onTick(long l) {
mTextCode.setText((l / 1000 )+ "秒后可重发");
}

@Override
public void onFinish() {
mTextCode.setEnabled(true);
mTextCode.setText("获取验证码");
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEtPhone = (EditText) findViewById(R.id.register_et_phone);
mEtCode = (EditText) findViewById(R.id.register_et_code);
mTextCode = (TextView) findViewById(R.id.register_text_code);
mEtPassWord = (EditText) findViewById(R.id.register_et_password);
mEtRePassWord = (EditText) findViewById(R.id.register_et_repassword);
mBtnSubmit = (Button) findViewById(R.id.register_btn_submit);
mTextCode.setOnClickListener(this);
mBtnSubmit.setOnClickListener(this);
}

@Override
public void onClick(View v) {
String phone = mEtPhone.getText().toString().trim();
String captcha = mEtCode.getText().toString().trim();
String password = mEtPassWord.getText().toString().trim();
String repassword = mEtRePassWord.getText().toString().trim();
switch (v.getId()) {
case R.id.register_text_code:
mTextCode.requestFocus();
if (!judgePhoneNums(phone)) {
return;
} else {
mCountDownTimer.start();
//这里的服务器接口是无效的需要换成你自己获取验证码的接口地址
RequestParams params = new RequestParams("http://api.php/Public/getSmsCode");
String str = String.valueOf(type);
//需要携带的参数
params.addBodyParameter("phone", phone);
params.addBodyParameter("type", str);
x.http().get(params, new Callback.CacheCallback<String>() {
@Override
public void onSuccess(String result) {
Log.e(TAG, result.toString());
}

@Override
public void onError(Throwable ex, boolean isOnCallback) {

}

@Override
public void onCancelled(CancelledException cex) {

}

@Override
public void onFinished() {

}

@Override
public boolean onCache(String result) {
return false;
}
});
}
break;
case R.id.register_btn_submit:
if (!judgePhoneNums(phone)) {
return;
} else if(TextUtils.isEmpty(captcha)) {
Toast.makeText(this, "验证码不能为空", Toast.LENGTH_SHORT).show();
} else if(password.length() < 6 ) {
Toast.makeText(this, "请输入长度大于6位的密码", Toast.LENGTH_SHORT).show();
} else if(!TextUtils.equals(password,repassword)) {
Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
} else if(mBtnSubmit == v) {
//这里的服务器接口是无效的需要换成你自己的注册接口地址
RequestParams params = new RequestParams("http://api.php/Public/smsRegister");
//需要带入的参数
params.addBodyParameter("phone", phone);
params.addBodyParameter("captcha", captcha);
params.addBodyParameter("password", password);
params.addBodyParameter("repassword", repassword);
x.http().post(params, new Callback.CacheCallback<String>() {
@Override
public void onSuccess(String result) {
Log.e(TAG, result.toString());
}

@Override
public void onError(Throwable ex, boolean isOnCallback) {

}

@Override
public void onCancelled(CancelledException cex) {

}

@Override
public void onFinished() {

}

@Override
public boolean onCache(String result) {
return false;
}
});
break;
}
}
}

private boolean judgePhoneNums(String phoneNums) {
if (isMatchLength(phoneNums, 11) && isMobileNO(phoneNums)) {
return true;
}
Toast.makeText(this, "请输入正确的手机号",Toast.LENGTH_SHORT).show();
return false;
}

/**
* 判断一个字符串的位数
* @param str
* @param length
* @return
*/

public static boolean isMatchLength(String str, int length) {
if (str.isEmpty()) {
return false;
} else {
return str.length() == length ? true : false;
}
}

/**
* 验证手机格式
*/

public static boolean isMobileNO(String mobileNums) {
String strTel = "[1][358]\\d{9}";
if (TextUtils.isEmpty(mobileNums))
return false;
else
return mobileNums.matches(strTel);
}
}

请求服务器打印的Log日志如下:
Android请求服务器获取短信验证码实现注册功能
最后的效果图如下:
Android请求服务器获取短信验证码实现注册功能