mvp的基本登录(简单实现)
来源:互联网 发布:软件产品质量缺陷等级 编辑:程序博客网 时间:2024/05/16 03:27
View包中
package view;/** * Created by lenovo on 2017/11/1. */public interface IUserView { /** * 登录成功 */ void onLoginSuccess(); /** * 登录失败 * * @param error */ void onLoginFailed(String error);}
model包中(两个类,IUserMode ,UserMode)
package model;import bean.User;/** * Created by lenovo on 2017/11/1. */public interface IUserMode { /** * 登录 * * @param user * @return 约定返回"true"为登录成功,其他为登录失败的错误信息 */ String login(User user);}
package model;import java.io.IOException;import java.util.HashMap;import bean.API;import bean.User;import bean.UserBean;import okhttp3.Call;import utils.GsonObjectCallback;import utils.OkHttp3Utils;import view.IUserView;/** * Created by lenovo on 2017/11/1. */public class UserMode implements IUserMode{ private String code; @Override public String login(User user) { HashMap<String,String> map=new HashMap<>(); map.put("mobile",user.getUsername()); map.put("password",user.getPassword()); OkHttp3Utils.getInstance().doPost(API.LOGIN, map, new GsonObjectCallback<UserBean>() { @Override public void onUi(UserBean userBean) { code = userBean.getCode(); } @Override public void onFailed(Call call, IOException e) { } }); /* boolean networkError = false; //网络是否异常 try { Thread.sleep(3000);//模拟网络连接 if (networkError) { return "网络异常"; } else if ("ethanco".equals(user.getUsername()) && "123456".equals(user.getPassword())) { return "true"; } else { return "账号或密码错误"; } } catch (InterruptedException e) { e.printStackTrace(); return e.getMessage(); }*/ return code; }}
presenter包中
package presenter;import android.os.Handler;import android.os.Looper;import bean.User;import model.UserMode;import view.IUserView;/** * Created by lenovo on 2017/11/1. */public class UserPresenter { private final IUserView userView; private final UserMode userMode; public UserPresenter(IUserView userView) { this.userView = userView; this.userMode = new UserMode(); } /** * 登录 * * @param user */ public void login(final User user) { /* new Thread() { @Override public void run() { final String res = userMode.login(user); new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { if ("true".equals(res)) { userView.onLoginSuccess(); } else { userView.onLoginFailed(res); } } }); } }.start();*/ final String res = userMode.login(user); if ("0".equals(res)) { userView.onLoginSuccess(); } else { userView.onLoginFailed(res); } }}
bean包中
package bean;/** * Created by lenovo on 2017/11/1. */public class User {(自己写) public User(String username, String password) { this.username = username; this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } private String username; private String password;}package bean;/** * Created by lenovo on 2017/11/1. */public class UserBean {(网络请求) /** * msg : 登录成功 * code : 0 * data : {"age":null,"appkey":null,"appsecret":null,"createtime":"2017-10-19T21:09:31","email":null,"gender":0,"icon":null,"mobile":"18401213718","money":0,"nickname":null,"password":"123456","token":null,"uid":1000,"username":"18401213718"} */ private String msg; private String code; private DataBean data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public DataBean getData() { return data; } public void setData(DataBean data) { this.data = data; } public static class DataBean { /** * age : null * appkey : null * appsecret : null * createtime : 2017-10-19T21:09:31 * email : null * gender : 0 * icon : null * mobile : 18401213718 * money : 0 * nickname : null * password : 123456 * token : null * uid : 1000 * username : 18401213718 */ private Object age; private Object appkey; private Object appsecret; private String createtime; private Object email; private int gender; private Object icon; private String mobile; private int money; private Object nickname; private String password; private Object token; private int uid; private String username; public Object getAge() { return age; } public void setAge(Object age) { this.age = age; } public Object getAppkey() { return appkey; } public void setAppkey(Object appkey) { this.appkey = appkey; } public Object getAppsecret() { return appsecret; } public void setAppsecret(Object appsecret) { this.appsecret = appsecret; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public Object getEmail() { return email; } public void setEmail(Object email) { this.email = email; } public int getGender() { return gender; } public void setGender(int gender) { this.gender = gender; } public Object getIcon() { return icon; } public void setIcon(Object icon) { this.icon = icon; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public int getMoney() { return money; } public void setMoney(int money) { this.money = money; } public Object getNickname() { return nickname; } public void setNickname(Object nickname) { this.nickname = nickname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Object getToken() { return token; } public void setToken(Object token) { this.token = token; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }}
MainActivity中
package com.bwie.mvp;import android.app.ProgressDialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import bean.User;import presenter.UserPresenter;import view.IUserView;public class MainActivity extends AppCompatActivity implements IUserView,View.OnClickListener{ private EditText username,password; private Button button; private UserPresenter userPresenter; private ProgressDialog loginProgreess; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); username= (EditText) findViewById(R.id.username); password= (EditText) findViewById(R.id.password); button= (Button) findViewById(R.id.login); button.setOnClickListener(this); userPresenter = new UserPresenter(this); } @Override public void onClick(View view) { String user = username.getText().toString().trim(); String pass= password.getText().toString().trim(); if (TextUtils.isEmpty(user) || TextUtils.isEmpty(pass)) { Toast.makeText(getApplicationContext(), "账号或密码不能为空", Toast.LENGTH_SHORT).show(); return; } loginProgreess = ProgressDialog.show(this, "登录", "正在登录..."); userPresenter.login(new User(user, pass)); } @Override public void onLoginSuccess() { loginProgreess.dismiss(); Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show(); } @Override public void onLoginFailed(String error) { loginProgreess.dismiss(); Toast.makeText(getApplicationContext(), "登录失败:" + error, Toast.LENGTH_SHORT).show(); }}
activity_main中
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.bwie.mvp.MainActivity"> <EditText android:id="@+id/username" android:layout_width="400dp" android:layout_height="wrap_content" /> <EditText android:id="@+id/password" android:layout_below="@id/username" android:layout_width="400dp" android:layout_height="wrap_content" /> <Button android:id="@+id/login" android:text="登录" android:layout_below="@id/password" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
阅读全文
0 0
- mvp的基本登录(简单实现)
- MVP简单的实现登录
- MVP简单登录实现
- 简单的用MVP实现登录界面
- MVP的简单登录
- MVP的简单登录
- 简单实现mvp模式登录
- MVP模式的简单介绍跟简单实现登录功能
- 使用MVP模式实现简单的登录功能
- Android MVP模式的简单登录
- MVP 一个简单的登录+判断
- MVP架构的简单实现
- mvp 简单的案例, mvp实现登录及注册 分为moudle数据层 activity视图v层 Presenter逻辑层
- 简单登录 MVP
- MVP的实现:登录为例
- 这是一个使用mvp模式实现模拟用户登录的简单Demo。
- android mvp实现登录
- MVP 实现登录注册
- 统计学习
- MySQL中的运算符,隐式转换,特有的limit语句
- static 关键字
- 51nod 1829 函数 (斯特林数)
- MVP模式实现接口登陆
- mvp的基本登录(简单实现)
- u-boot支持LCD显示(基于TQ2440)
- 51nod 1791 合法括号子段 (队列)
- SELECT语句详解
- 二分法查找已排序数列中目标数字的位置
- Python学习笔记——模块
- java作业day10/1
- 前端学习概述
- 【实战\聚焦Python分布式爬虫必学框架Scrapy 打造搜索引擎项目笔记】第1章 课程介绍