MVP注册登录+OkHttp网络请求+展示数据

left_item_font_selector.xml
xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorRed" android:state_pressed="true">item>
    <item android:color="@color/colorBlack" android:state_pressed="false">item>
selector>
 
  
left_item_selector.xml
 
  
xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/colorDark" android:state_pressed="true">item>
    <item android:drawable="@color/colorWhite" android:state_pressed="false">item>
selector>
 
  
color.xml-value
 
  
xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5color>
    <color name="colorPrimaryDark">#303F9Fcolor>
    <color name="colorAccent">#FF4081color>
    <color name="colorRed">#ff3660color>
    <color name="colorDark">#EEEcolor>
    <color name="colorWhite">#FFFFFFcolor>
    <color name="colorBlack">#FF262426color>
resources>
 
  
activity_main.xml
 
  
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.bwie.jingdong.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:text="18310830365"
            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:text="123456"
            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>
 
  
 
  
activity_register.xml
xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.bwie.jingdong.view.RegisterActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp">

        <ImageView
            android:id="@+id/iv_back1"
            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_phone1"
            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_pwd1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="请输入密码" />

        <ImageView
            android:id="@+id/iv_pwd1"
            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_register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:text="注册" />

LinearLayout >
 
  
 
  
activity_class.xml
 
  
 
  
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bwie.jingdong.view.ClassActivity">
    <ListView
        android:id="@+id/lv_left"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:scrollbars="none">ListView>

    <ListView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4">ListView>
LinearLayout>
activity_base.xml
xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.bwie.jingdong.view.BaseActivity">

android.support.constraint.ConstraintLayout>
 
  
 
  
left_item.xml
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center" />
LinearLayout>
 
  
 
  

//HttpUtil
package com.bwie.jingdong.net;

import java.util.Map;

import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Created by user on 2017/11/8.
 * 网络请求工具类
 * 1.先导入OkHttpClient的jar包,然后导入com.squareup.okhttp3:logging-interceptor拦截器的包
 * 2.完善HttpUtil的工具类(1).getHttpUtil双层判断
 *                      (2).HttpUtil,创建OkHttpClient
 */

public class HttpUtil {
    private static HttpUtil httpUtil;
    private final OkHttpClient client;

    private HttpUtil() {
        //创建拦截器
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        //
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        //创建OkHttp
        client = new OkHttpClient.Builder()
                .addInterceptor(logging)
                .build();
    }

    public static HttpUtil getHttpUtil() {
        if (httpUtil == null) {
            synchronized (HttpUtil.class) {
                if (httpUtil == null) {
                    httpUtil = new HttpUtil();
                }
            }
        }
        return httpUtil;
    }

    /**
     * GET请求
     *
     * @param url      请求地址
     * @param callback 回调
     */
    public void doGet(String url, Callback callback) {
        Request request = new Request.Builder().url(url).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * post请求
     *
     * @param url      请求地址
     * @param params   请求参数
     * @param callback 回调
     */
    public void doPost(String url, Map params, Callback callback) {
        //这里可以加网络判断

        //判断参数
        if (params == null || params.size() ==0){
            //运行时异常
            throw new RuntimeException("params is null!!!");
        }
        //创建Request
        FormBody.Builder builder = new FormBody.Builder();
        for (Map.Entry 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);
    }
}
 
  
OnNetListener
package com.bwie.jingdong.net;

/**
 * Created by user on 2017/11/8.
 */

public interface OnNetListener<T> {
    //成功回调
    public void onSuccess(T t);

    //失败回调
    public void onFailure(Exception e);
}
 
  
API
package com.bwie.jingdong.net;

/**
 * Created by user on 2017/11/8.
 */

public interface Api {
    public static boolean isOnline = false;
    public static final String DEV = "http://120.27.23.105/";
    public static final String WROK = "";
    public static final String HOST = isOnline ? WROK : DEV;

    public static final String LOGIN = HOST+"user/login";//登陆
    public static final String REGISTER = HOST + "user/reg";//注册
    public static final String CLASS = HOST + "product/getCatagory";//分类
    public static final String PRODUCT_CATAGORY = HOST + "product/getProductCatagory";//商品子分类接口
}
 
  
ActivityStroreUtil
 
  
package com.bwie.jingdong.utils;

import android.app.Activity;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by user on 2017/11/8.
 * 用于保存已经开启的Activity
 */

public class ActivityStroreUtil {
    private static List list = new LinkedList<>();

    /**
     * 用集合保存所有开启的Activity
     *
     * @param ac
     */
    public static void addActivity(Activity ac){
        list.add(ac);
    }
    /**
     * 关闭已经开启的Activity
     */
    public void finishAll(){
        for (Activity ac:list) {
            if (ac != null)
                ac.finish();
        }
    }
}
 
  
BaseModel
 
  
package com.bwie.jingdong.model;

import android.os.Handler;
import android.os.Looper;

/**
 * Created by user on 2017/11/8.
 */

public class BaseModel {
    protected Handler handler = new Handler(Looper.getMainLooper());
}
 
  
ClassModel
 
  
package com.bwie.jingdong.model;

import com.bwie.jingdong.bean.BaseBean;
import com.bwie.jingdong.bean.Catagory;
import com.bwie.jingdong.bean.ProductCatagoryBean;
import com.bwie.jingdong.model.IModel.IClassModel;
import com.bwie.jingdong.net.Api;
import com.bwie.jingdong.net.HttpUtil;
import com.bwie.jingdong.net.OnNetListener;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
 * Created by user on 2017/11/8.
 * 获取分类的model
 */

public class ClassModel extends BaseModel implements IClassModel {
    @Override
    public void getCatagory(final OnNetListener onNetListener) {
        HttpUtil.getHttpUtil().doGet(Api.CLASS, 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 Catagory catagory = new Gson().fromJson(string, Catagory.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onNetListener.onSuccess(catagory);
                    }
                });
            }
        });
    }

    @Override
    public void getProductCatagory(String cid, final OnNetListener onNetListener) {
        Map params = new HashMap<>();
        params.put("cid", cid);
        HttpUtil.getHttpUtil().doPost(Api.PRODUCT_CATAGORY, 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 ProductCatagoryBean productCatagoryBean = new Gson().fromJson(string, ProductCatagoryBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onNetListener.onSuccess(productCatagoryBean);
                    }
                });
            }
        });
    }
}
 
  
IClassModel
package com.bwie.jingdong.model.IModel;

import com.bwie.jingdong.bean.BaseBean;
import com.bwie.jingdong.bean.Catagory;
import com.bwie.jingdong.bean.ProductCatagoryBean;
import com.bwie.jingdong.net.OnNetListener;

/**
 * Created by user on 2017/11/8.
 */

public interface IClassModel {
    public void getCatagory(OnNetListener onNetListener);

    public void getProductCatagory(String cid, OnNetListener onNetListener);
}
 
  
 
  
LoginModel
 
  
package com.bwie.jingdong.model;

import com.bwie.jingdong.bean.LoginBean;
import com.bwie.jingdong.model.IModel.ILoginModel;
import com.bwie.jingdong.net.Api;
import com.bwie.jingdong.net.HttpUtil;
import com.bwie.jingdong.net.OnNetListener;
import com.google.gson.Gson;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;


import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by user on 2017/11/8.
 */

public class LoginModel extends BaseModel implements ILoginModel {
    @Override
    public void login(String account, String pwd, final OnNetListener onNetListener) {
        Map params = new HashMap<>();
        params.put("mobile", account);
        params.put("password", pwd);
        HttpUtil.getHttpUtil().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);
                    }
                });
            }
        });
    }
}
 
  
ILoginModel
 
  
package com.bwie.jingdong.model.IModel;

import com.bwie.jingdong.bean.LoginBean;
import com.bwie.jingdong.net.OnNetListener;

/**
 * Created by user on 2017/11/8.
 * 网络请求回调接口
 */

public interface ILoginModel {
    public void login(String account, String pwd, OnNetListener onNetListener);
}
 
  
RegisterModel
 
  
package com.bwie.jingdong.model;

import com.bwie.jingdong.bean.BaseBean;
import com.bwie.jingdong.model.IModel.IRegisterModel;
import com.bwie.jingdong.net.Api;
import com.bwie.jingdong.net.HttpUtil;
import com.bwie.jingdong.net.OnNetListener;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by user on 2017/11/8.
 * 真正的网络请求,去注册
 */

public class RegisterModel extends BaseModel implements IRegisterModel {
    @Override
    public void register(String account, String pwd, final OnNetListener onNetListener) {
        Map params = new HashMap<>();
        params.put("mobile", account);
        params.put("password", pwd);
        HttpUtil.getHttpUtil().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);
                    }
                });
            }
        });
    }
}
 
  
 
  
IRegisterModel
package com.bwie.jingdong.model.IModel;

import com.bwie.jingdong.bean.BaseBean;
import com.bwie.jingdong.net.OnNetListener;

/**
 * Created by user on 2017/11/8.
 */

public interface IRegisterModel {
    public void register(String account, String pwd, OnNetListener onNetListener);
}
 
  
ClassPresenter
package com.bwie.jingdong.presenter;

import com.bwie.jingdong.bean.Catagory;
import com.bwie.jingdong.bean.ProductCatagoryBean;
import com.bwie.jingdong.model.ClassModel;
import com.bwie.jingdong.model.IModel.IClassModel;
import com.bwie.jingdong.net.OnNetListener;
import com.bwie.jingdong.view.IView.IClassActivity;

import java.util.List;

/**
 * Created by user on 2017/11/8.
 */

public class ClassPresenter {
    private final IClassModel iclassModel;
    private IClassActivity iClassActivity;

    public ClassPresenter(IClassActivity iClassActivity) {
        this.iClassActivity = iClassActivity;
        iclassModel = new ClassModel();
    }

    //获取分类
    public void getCatagory() {
        iclassModel.getCatagory(new OnNetListener() {
            @Override
            public void onSuccess(Catagory catagory) {
                iClassActivity.showData(catagory.getData());
                //拿到右侧第一条数据
                List dataBean = catagory.getData();
                int cid = dataBean.get(0).getCid();
                iclassModel.getProductCatagory(cid + "", new OnNetListener() {
                    @Override
                    public void onSuccess(ProductCatagoryBean productCatagoryBean) {

                    }
                    @Override
                    public void onFailure(Exception e) {

                    }
                });
            }
            @Override
            public void onFailure(Exception e) {

            }
        });
    }
}
 
  
 
  
MainPresenter
package com.bwie.jingdong.presenter;

import android.text.TextUtils;

import com.bwie.jingdong.bean.LoginBean;
import com.bwie.jingdong.model.IModel.ILoginModel;
import com.bwie.jingdong.model.IModel.IRegisterModel;
import com.bwie.jingdong.model.LoginModel;
import com.bwie.jingdong.model.RegisterModel;
import com.bwie.jingdong.net.OnNetListener;
import com.bwie.jingdong.view.IView.IMainActivity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by user on 2017/11/8.
 */

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() {
                @Override
                public void onSuccess(LoginBean loginBean) {
                    //保存登陆成功后的数据,可以保存到SP,也可以保存到数据库
                    iMainActivity.show(loginBean.getMsg());
                    //跳转到分类界面
                    iMainActivity.toClassAc();
                }

                @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();
    }
}
 
  
 
  
RegisterPresenter
package com.bwie.jingdong.presenter;

import android.text.TextUtils;

import com.bwie.jingdong.bean.BaseBean;
import com.bwie.jingdong.model.IModel.IRegisterModel;
import com.bwie.jingdong.model.RegisterModel;
import com.bwie.jingdong.net.OnNetListener;
import com.bwie.jingdong.view.IView.IRegisterActivity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by user on 2017/11/8.
 */

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() {
                @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) {

                }
            });
        }

    }
}
 
  
BaseActivity
package com.bwie.jingdong.view;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.bwie.jingdong.utils.ActivityStroreUtil;

import java.util.LinkedList;
import java.util.List;

public class BaseActivity extends AppCompatActivity {

    private List activityList = new LinkedList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityStroreUtil.addActivity(this);
    }
}
 
  
 
  
 
  
ClassActivity
package com.bwie.jingdong.view;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bwie.jingdong.R;
import com.bwie.jingdong.adapter.LeftAdapter;
import com.bwie.jingdong.bean.Catagory;
import com.bwie.jingdong.presenter.ClassPresenter;
import com.bwie.jingdong.view.IView.IClassActivity;

import java.util.List;

public class ClassActivity extends AppCompatActivity implements IClassActivity {
    private ListView mLvLeft;
    private ClassPresenter classPresenter;
    private LeftAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class);
        initView();
        //去P层,调用getCatagory
        classPresenter.getCatagory();

        //给listview 设置点击事件
        mLvLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                adapter.press(position);
                //请求对应的右侧数据
            }
        });
    }

    private void initView() {
        mLvLeft = (ListView) findViewById(R.id.lv_left);
    }

    @Override
    public void showData(List list) {
        //创建适配器
        adapter = new LeftAdapter(this, list);
        mLvLeft.setAdapter(adapter);
    }
}
IClassActivity
package com.bwie.jingdong.view.IView;

import com.bwie.jingdong.bean.Catagory;

import java.util.List;

/**
 * Created by user on 2017/11/8.
 */

public interface IClassActivity {
    public void showData(List list);
}
 
  
 
  
MainActivity
package com.bwie.jingdong.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bwie.jingdong.R;
import com.bwie.jingdong.presenter.MainPresenter;
import com.bwie.jingdong.view.IView.IMainActivity;

public class MainActivity extends BaseActivity implements View.OnClickListener,IMainActivity {

    private ImageView mIvBack;
    /**
     * 18310830365
     */
    private EditText mEtPhone;
    /**
     * 123456
     */
    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);
        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);
    }

    @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);
    }

    @Override
    public void toClassAc() {
        Intent intent = new Intent(MainActivity.this, ClassActivity.class);
        startActivity(intent);
    }
}
 
  
IMainActivity
 
  
package com.bwie.jingdong.view.IView;

/**
 * Created by user on 2017/11/8.
 */

public interface IMainActivity {
    public String getAccount();

    public String getPwd();

    public void show(String str);

    public void toRegisterAc();

    public void toClassAc();
}
RegisterActivity
package com.bwie.jingdong.view;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.bwie.jingdong.R;
import com.bwie.jingdong.presenter.RegisterPresenter;
import com.bwie.jingdong.view.IView.IRegisterActivity;

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);
        initView();
    }

    private void initView() {
        mIvBack = (ImageView) findViewById(R.id.iv_back1);
        mEtPhone = (EditText) findViewById(R.id.et_phone1);
        mEtPwd = (EditText) findViewById(R.id.et_pwd1);
        mIvPwd = (ImageView) findViewById(R.id.iv_pwd1);
        mBtLogin = (Button) findViewById(R.id.bt_register1);
        mBtLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.bt_register:
                //注册
                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();
    }
}
IRegisterActivity
 
  
package com.bwie.jingdong.view.IView;

/**
 * Created by user on 2017/11/8.
 */

public interface IRegisterActivity {
    public String getAccount();

    public String getPwd();

    public void finishAc();

    public void show(String str);
}

你可能感兴趣的:(MVP注册登录+OkHttp网络请求+展示数据)