Android中登录注册(第三方)
来源:互联网 发布:js字符串转utf8编码 编辑:程序博客网 时间:2024/05/17 09:04
1.首先我们创建布局,登录的布局
<?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="mvpframework.bwie.com.a1509aproject.view.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<ImageView
android:id="@+id/iv_back"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:background="@drawable/cha" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="京东登陆" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="账号" />
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:hint="手机号" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="密码" />
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入密码" />
<ImageView
android:id="@+id/iv_pwd"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="15dp"
android:background="@drawable/yannormal" />
</LinearLayout>
<Button
android:id="@+id/bt_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="登陆" />
<TextView
android:id="@+id/tv_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="请注册" />
</LinearLayout>
2.注册的布局
<?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="mvpframework.bwie.com.a1509aproject.view.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<ImageView
android:id="@+id/iv_back"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:background="@drawable/cha" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="京东注册" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="账号" />
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:hint="手机号" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="密码" />
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入密码" />
<ImageView
android:id="@+id/iv_pwd"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="15dp"
android:background="@drawable/yannormal" />
</LinearLayout>
<Button
android:id="@+id/bt_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="注册" />
</LinearLayout>
3.我们用到的理念是mvp,首先创建model层,具体如下:
login的model层
public class LoginModel extends BaseModel implements ILoginModel {
@Override
public void login(String account, String pwd, final OnNetListener<LoginBean> onNetListener) {
Map<String, String> params = new HashMap<>();
params.put("mobile", account);
params.put("password", pwd);
HttpUtils.getHttpUtils().doPost(Api.LOGIN, params, new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
onNetListener.onFailure(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
final LoginBean loginBean = new Gson().fromJson(string, (Type) LoginBean.class);
handler.post(new Runnable() {
@Override
public void run() {
onNetListener.onSuccess(loginBean);
}
});
}
});
}
}
登录的接口
public interface ILoginModel {
public void login(String account, String pwd, OnNetListener<LoginBean> onNetListener);
}
注册的model层
public class RegisterModel extends BaseModel implements IRegisterModel {
@Override
public void register(String account, String pwd, final OnNetListener<BaseBean> onNetListener) {
Map<String, String> params = new HashMap<>();
params.put("mobile", account);
params.put("password", pwd);
HttpUtils.getHttpUtils().doPost(Api.REGISTER, params, new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
onNetListener.onFailure(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
final BaseBean baseBean = new Gson().fromJson(string, BaseBean.class);
handler.post(new Runnable() {
@Override
public void run() {
onNetListener.onSuccess(baseBean);
}
});
}
});
}
}
注册的接口
public interface IRegisterModel {
public void register(String account, String pwd, OnNetListener<BaseBean> onNetListener);
}
再次创建presenter层
登录的presenter层
public class MainPresenter {
private IMainActivity iMainActivity;
private final ILoginModel iLoginModel;
private final IRegisterModel iRegisterModel;
public MainPresenter(IMainActivity iMainActivity) {
this.iMainActivity = iMainActivity;
iLoginModel = new LoginModel();
iRegisterModel = new RegisterModel();
}
public void login() {
String account = iMainActivity.getAccount();
String pwd = iMainActivity.getPwd();
//判断账号密码是否正确
if (checkAccount(account) && checkPwd(pwd)) {
//去调用model,进行登陆
iLoginModel.login(account, pwd, new OnNetListener<LoginBean>() {
@Override
public void onSuccess(LoginBean loginBean) {
//保存登陆成功后的数据,可以保存到SP,也可以保存到数据库
iMainActivity.show(loginBean.getMsg());
}
@Override
public void onFailure(Exception e) {
}
});
}
}
private boolean checkPwd(String pwd) {
if (TextUtils.isEmpty(pwd)) {
//给用户提示,输入的账号不能为空
iMainActivity.show("请输入密码");
return false;
}
if (pwd.length() != 6) {
iMainActivity.show("请输入6位密码");
return false;
}
return true;
}
/**
* 验证手机号是否正确
*
* @param account
*/
private boolean checkAccount(String account) {
if (TextUtils.isEmpty(account)) {
//给用户提示,输入的账号不能为空
iMainActivity.show("请输入账号");
return false;
}
if (!isMobileNO(account)) {
iMainActivity.show("请输入正确的手机号");
return false;
}
return true;
}
/*
判断是否是手机号
*/
public static boolean isMobileNO(String mobiles) {
Pattern p = Pattern.compile("^(13[0-9]|14[57]|15[0-35-9]|17[6-8]|18[0-9])[0-9]{8}$");
Matcher m = p.matcher(mobiles);
return m.matches();
}
public void register() {
//其实就是跳转到注册页面
iMainActivity.toRegisterAc();
}
}
注册的presenter层
public class RegisterPresenter {
private IRegisterActivity iRegisterActivity;
private final IRegisterModel iRegisterModel;
public RegisterPresenter(IRegisterActivity iRegisterActivity) {
this.iRegisterActivity = iRegisterActivity;
iRegisterModel = new RegisterModel();
}
private boolean checkPwd(String pwd) {
if (TextUtils.isEmpty(pwd)) {
//给用户提示,输入的账号不能为空
iRegisterActivity.show("请输入密码");
return false;
}
if (pwd.length() != 6) {
iRegisterActivity.show("请输入6位密码");
return false;
}
return true;
}
/**
* 验证手机号是否正确
*
* @param account
*/
private boolean checkAccount(String account) {
if (TextUtils.isEmpty(account)) {
//给用户提示,输入的账号不能为空
iRegisterActivity.show("请输入账号");
return false;
}
if (!isMobileNO(account)) {
iRegisterActivity.show("请输入正确的手机号");
return false;
}
return true;
}
/*
判断是否是手机号
*/
public static boolean isMobileNO(String mobiles) {
Pattern p = Pattern.compile("^(13[0-9]|14[57]|15[0-35-9]|17[6-8]|18[0-9])[0-9]{8}$");
Matcher m = p.matcher(mobiles);
return m.matches();
}
public void register() {
String account = iRegisterActivity.getAccount();
String pwd = iRegisterActivity.getPwd();
//判断账号密码是否正确
if (checkAccount(account) && checkPwd(pwd)) {
iRegisterModel.register(account, pwd, new OnNetListener<BaseBean>() {
@Override
public void onSuccess(BaseBean baseBean) {
//成功以后,回到登陆界面
if (baseBean.getCode().equals("1")) {
iRegisterActivity.show(baseBean.getMsg());
} else {
iRegisterActivity.show(baseBean.getMsg());
iRegisterActivity.finishAc();
}
}
@Override
public void onFailure(Exception e) {
}
});
}
}
}
最后是view层
登录的view层
public class MainActivity extends BaseActivity implements View.OnClickListener, IMainActivity {
private ImageView mIvBack;
/**
* 手机号
*/
private EditText mEtPhone;
/**
* 请输入密码
*/
private EditText mEtPwd;
private ImageView mIvPwd;
/**
* 登陆
*/
private Button mBtLogin;
/**
* 请注册
*/
private TextView mTvRegister;
private MainPresenter mainPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//关联presenter
mainPresenter = new MainPresenter(this);
initView();
}
private void initView() {
mIvBack = (ImageView) findViewById(R.id.iv_back);
mEtPhone = (EditText) findViewById(R.id.et_phone);
mEtPwd = (EditText) findViewById(R.id.et_pwd);
mIvPwd = (ImageView) findViewById(R.id.iv_pwd);
mBtLogin = (Button) findViewById(R.id.bt_login);
mBtLogin.setOnClickListener(this);
mTvRegister = (TextView) findViewById(R.id.tv_register);
mTvRegister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_login:
mainPresenter.login();
break;
case R.id.tv_register:
//注册
mainPresenter.register();
break;
}
}
@Override
public String getAccount() {
return mEtPhone.getText().toString().trim();
}
@Override
public String getPwd() {
return mEtPwd.getText().toString().trim();
}
@Override
public void show(String str) {
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
@Override
public void toRegisterAc() {
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(intent);
}
}
view层接口
public interface IMainActivity {
//获取手机号
public String getPhone();
//获取密码
public String getPwd();
//显示登陆成功后的数据
public void show(String str);
//跳转
public void toSecond();
}
注册的view层
public class RegisterActivity extends AppCompatActivity implements IRegisterActivity, View.OnClickListener {
private ImageView mIvBack;
/**
* 手机号
*/
private EditText mEtPhone;
/**
* 请输入密码
*/
private EditText mEtPwd;
private ImageView mIvPwd;
/**
* 注册
*/
private Button mBtLogin;
private RegisterPresenter registerPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
registerPresenter = new RegisterPresenter(this);
initView();
}
private void initView() {
mIvBack = (ImageView) findViewById(R.id.iv_back);
mEtPhone = (EditText) findViewById(R.id.et_phone);
mEtPwd = (EditText) findViewById(R.id.et_pwd);
mIvPwd = (ImageView) findViewById(R.id.iv_pwd);
mBtLogin = (Button) findViewById(R.id.bt_login);
mBtLogin.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
case R.id.bt_login:
//注册
registerPresenter.register();
break;
}
}
@Override
public String getAccount() {
return mEtPhone.getText().toString().trim();
}
@Override
public String getPwd() {
return mEtPwd.getText().toString().trim();
}
@Override
public void finishAc() {
finish();
}
@Override
public void show(String str) {
Toast.makeText(RegisterActivity.this, str, Toast.LENGTH_SHORT).show();
}
}
baseModel中创建handler对象
public class BaseModel {
protected Handler handler = new Handler(Looper.getMainLooper());
}
net文件夹
public class Api {
public static final boolean IS_ONLINE = true;
public static final String DEV = "http://169.254.222.37";//开发时用的ip
public static final String ONLINE = "http://120.27.23.105";//线上的ip
public static final String HOST = IS_ONLINE ? ONLINE : DEV;
public static final String LOGIN = HOST + "/user/login";
public static final String REGISTER = HOST + "/user/reg";
}
public class HttpUtils {
public final OkHttpClient client;
public static HttpUtils httpUtils;
private HttpUtils(){
//创建拦截器
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
client = new OkHttpClient.Builder().addInterceptor(logging)
.build();
}
public static HttpUtils getHttpUtils(){
if(httpUtils==null){
synchronized (HttpUtils.class){
if(httpUtils==null){
httpUtils = new HttpUtils();
}
}
}
return httpUtils;
}
//get请求
public void doGet(String url, Callback callback){
Request build = new Request.Builder().build();
client.newCall(build).enqueue(callback);
}
//post请求
public void doPost(String url, Map<String,String>params,Callback callback){
if(params==null||params.size()==0){
throw new RuntimeException("params is null");
}
FormBody.Builder builder = new FormBody.Builder();
//遍历集合
for(Map.Entry<String,String> entry:params.entrySet()){
builder.add(entry.getKey(),entry.getValue());
}
FormBody formBody = builder.build();
Request request = new Request.Builder().url(url).post(formBody).build();
client.newCall(request).enqueue(callback);
}
}
public interface OnNetListener<T>{
public void onSuccess(T t);
public void onFailure(Exception e);
}
- Android中登录注册(第三方)
- Android中FaceBook第三方登录集成
- Android 第三方登录
- Android 第三方登录
- Android第三方登录
- android 第三方登录
- Android 第三方登录
- android QQ第三方登录(一)
- Android QQ第三方登录(二)
- dz论坛登录注册-dz论坛第三方登录注册
- django 注册、登录、第三方接口程序
- Android QQ第三方登录
- android,QQ第三方登录
- Android第三方账号登录
- Android QQ第三方登录
- Android实现第三方登录
- Android QQ第三方登录
- Facebook android 第三方登录
- 习题6.1(2)
- C#程序调用Form.Show报“创建窗口句柄出错”
- Arduino使用温湿度传感器DHT11
- 设计模式-单例模式
- Arduino使用温湿度传感器DHT11(补充说明)
- Android中登录注册(第三方)
- Arduino使用LCD1602
- Arduino使用步进电机
- Hbase的单机版和高可用版本
- Arduino通过L298N红板控制板控制直流电机
- SharpDevelop源码分析之插件
- 重构就是力量——我为重构改编了首歌
- Arduino控制SD卡(一)
- 多线程断点下载