BaseActivity:
public abstract class BaseActitivty<P extends BasePresenter> extends AppCompatActivity implements IBaseView{
public P presenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(setChildContentView());
initView();
initBaseData();
initData();
}
private void initBaseData(){
presenter = setPresenter();
if (presenter != null) {
presenter.attachView(this);
}else {
try {
throw new Exception("您在Activity中没有设置Presenter,请重新创建");
} catch (Exception e) {
e.printStackTrace();
}
}
}
abstract P setPresenter();
abstract void initView();
abstract void initData();
abstract int setChildContentView();
}
----------------------------------------------------------------------------------
BasePresenter:
public class BasePresenter<V extends IBaseView>{
private V miBaseView;
public void attachView(V iBaseView){
this.miBaseView = iBaseView;
}
public V getView(){
return miBaseView;
}
public void detach(){
miBaseView = null;
}
}
-------------------------------------------------------------------------------------
Mactivity:
public class MainActivity extends BaseActitivty<MainPresenter> implements IMainView, View.OnClickListener {
private EditText mobile;
private EditText password;
private EditText password_sure;
private Button register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
/* presenter = new MainPresenter();
presenter.attachView(this);
presenter.getDataFromServer();*/
}
@Override
protected MainPresenter setPresenter() {
return new MainPresenter();
}
@Override
void initView() {
mobile = findViewById(R.id.mobile);
password = findViewById(R.id.password);
password_sure = findViewById(R.id.password_sure);
register = findViewById(R.id.register);
register.setOnClickListener(this);
findViewById(R.id.login_in).setOnClickListener(this);
}
@Override
void initData() {
//presenter.getDataFromServer();
}
@Override
public int setChildContentView(){
return R.layout.activity_main;
}
@Override
public void onSuccess(String str) {
}
@Override
protected void onDestroy() {
super.onDestroy();
presenter.detach();
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.register:
String phoneNum = mobile.getText().toString();
String passwor = password.getText().toString();
String passwor_sure = password_sure.getText().toString();
if (!CommonUtil.isMobileNO(phoneNum)){
Toast.makeText(MainActivity.this,"手机号格式不正确",Toast.LENGTH_SHORT).show();
return;
}
if (passwor == null ||"".equals(passwor)){
Toast.makeText(MainActivity.this,"密码格式不对",Toast.LENGTH_SHORT).show();
return;
}
if (!passwor.equals(passwor_sure)){
Toast.makeText(MainActivity.this,"两次密码不一致",Toast.LENGTH_SHORT).show();
return;
}
if (presenter != null){
String path = "user/reg";
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put("mobile",phoneNum);
hashMap.put("password",passwor_sure);
presenter.getDataFromServer(path,hashMap);
}
break;
case R.id.login_in:
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
break;
}
}
}
MainPresenter:
public class MainPresenter extends BasePresenter<IMainView>{
private final HttpUtils httpUtils;
private IMainView view;
public MainPresenter(){
httpUtils = HttpUtils.getInstance();
}
//注册进行跳转
public void getDataFromServer(String path, HashMap<String,String> hashMap){
httpUtils.doPost(path, hashMap, new HttpUtilsCallback() {
@Override
public void onSuccess(String str) {
Log.e("zer","str="+str);
Looper.prepare();
view = getView();
Gson gson = new Gson();
Bean bean = gson.fromJson(str, Bean.class);
String code = bean.getCode();
String msg = bean.getMsg();
if (code.equals("1")){
Toast.makeText((Context) view,msg+"",Toast.LENGTH_SHORT).show();
}else {
Intent intent = new Intent((Context) view, LoginActivity.class);
((Context) view).startActivity(intent);
}
Looper.loop();
}
@Override
public void onFail(int errCode, String errMsg) {
}
});
}
}
-------------------------------------------------------------------------------------------------------
IMainView:
public interface IMainView extends IBaseView{
int setChildContentView();
void onSuccess(String str);
}
-------------------------------------------------------------------------------------------------
LoginActivity:
public class LoginActivity extends BaseActitivty<LoginPresenter> implements ILoginView, View.OnClickListener {
private EditText edit_name;
private EditText edit_password;
private Button login;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
LoginPresenter setPresenter() {
return new LoginPresenter();
}
@Override
void initView() {
edit_name = findViewById(R.id.edit_name);
edit_password = findViewById(R.id.edit_password);
login = findViewById(R.id.login);
login.setOnClickListener(this);
}
@Override
void initData() {
}
@Override
int setChildContentView() {
return R.layout.activity_login;
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.login:
String name = edit_name.getText().toString();
String pwd = edit_password.getText().toString();
if (pwd == null || pwd == ""){
Toast.makeText(LoginActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
}
if (presenter != null){
String path = "user/login";
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put("mobile",name);
hashMap.put("password",pwd);
presenter.login(path,hashMap);
}
break;
}
}
@Override
public void onSuccess(String str) {
}
}
LoginPresenter:
public class LoginPresenter extends BasePresenter<ILoginView> {
private final HttpUtils instance;
public LoginPresenter(){
instance = HttpUtils.getInstance();
}
public void login(String path, HashMap<String,String> map){
instance.doPost(path,map,new HttpUtilsCallback(){
@Override
public void onSuccess(String str) {
Log.e("zer","str="+str);
Looper.prepare();
ILoginView view = getView();
Gson gson = new Gson();
Bean bean = gson.fromJson(str, Bean.class);
String code = bean.getCode();
String msg = bean.getMsg();
if (code.equals("1") ){
Toast.makeText((Context) view,""+msg,Toast.LENGTH_SHORT).show();
}else {
Intent intent = new Intent((Context) view, HomeActivity.class);
((Context) view).startActivity(intent);
}
Looper.loop();
}
@Override
public void onFail(int errCode, String errMsg) {
}
});
}
}
--------------------------------------------------------------------------
HttpUtils:
public class HttpUtils implements Callback{
private static HttpUtils INSTANCE;
private final OkHttpClient okHttpClient;
private HttpUtilsCallback mHttpUtilsCallback;
String url = "https://www.zhaoapi.cn/user/login?mobile=asd&password=123456";
public HttpUtils() {
okHttpClient = new OkHttpClient.Builder().build();
}
public static HttpUtils getInstance(){
if (INSTANCE == null){
INSTANCE = new HttpUtils();
}
return INSTANCE;
}
public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
public String doGet(String path){
Request request = new Request.Builder().url(Constant.BASE_URL+path).build();
Call call = okHttpClient.newCall(request);
call.enqueue(this);
return "ZhengEner";
}
public void doPost(String path, HashMap<String,String> map, HttpUtilsCallback httpUtilsCallback){
this.mHttpUtilsCallback = httpUtilsCallback;
FormBody.Builder builder = new FormBody.Builder();
Iterator<String> iterator = map.keySet().iterator();
while (iterator.hasNext()){
String key = iterator.next();
String value = map.get(key);
builder.add(key,value);
}
FormBody formBody = builder.build();
Request build = new Request.Builder()
.url(Constant.BASE_URL+path)
.post(formBody)
.build();
Call call = okHttpClient.newCall(build);
call.enqueue(this);
}
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
mHttpUtilsCallback.onSuccess(string);
}
}
Constant:
public class Constant {
public static final String BASE_URL = "https://www.zhaoapi.cn/";
}
-------------------------------------------------------------------
HttpUtilsCallback:
public interface HttpUtilsCallback {
void onSuccess(String str);
void onFail(int errCode,String errMsg);
}
-------------------------------------------------------------------
CommonUtil:
/*
*用正则表达式判断手机号:
*/
public class CommonUtil {
public static boolean isMobileNO(String mobile){
Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
Matcher m = p.matcher(mobile);
System.out.println(m.matches()+"-------");
return m.matches();
}
}