mvp+okhttp注册登录

//导包
compile 'com.google.code.gson:gson:2.6.2'

    compile 'com.squareup.okhttp3:okhttp:3.3.0'

    compile 'com.github.bumptech.glide:glide:3.7.0'

    compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
//activity_main



    

    

    

    

    

        
//activity_reg



    

    

    

    


        
//登录回调方法
//ILoginModel
package zhaojunchao.bawie.com.zhaojunchao20180423.model;

import java.util.Map;

/**
 * 登录的M层 by ASUS on 2018/4/23.
 */

public interface ILoginModel {
    //获取数据的回调方法
    void showLoginJson(String url, Map map, ILoginModelListener iLoginModelListener);
}
//注册回调方法
//IRegModel
package zhaojunchao.bawie.com.zhaojunchao20180423.model;

import java.util.Map;

/**
 * 登录的M层 by ASUS on 2018/4/23.
 */

public interface IRegModel {
    //获取数据的回调方法
    void showRegnJson(String url, Map map, IRegModelListener iRegModelListener);
}
//登录监听
//ILoginModelListener
package zhaojunchao.bawie.com.zhaojunchao20180423.model;

/**
 * 登陆的监听 by ASUS on 2018/4/23.
 */

public interface ILoginModelListener {
    //获取数据成功
    void showLoginJsonSuccess(String json);
    //获取数据失败
    void showLoginJsonError(String error);
}
//注册监听
//IRegModelListener
package zhaojunchao.bawie.com.zhaojunchao20180423.model;

/**
 * 登陆的监听 by ASUS on 2018/4/23.
 */

public interface IRegModelListener {
    //获取数据成功
    void showRegJsonSuccess(String json);
    //获取数据失败
    void showRegJsonError(String error);
}
//登录实现类
//LoginModelImpl
package zhaojunchao.bawie.com.zhaojunchao20180423.model;

import com.google.gson.Gson;

import java.util.Map;

import zhaojunchao.bawie.com.zhaojunchao20180423.utils.FromHttpListener;
import zhaojunchao.bawie.com.zhaojunchao20180423.utils.HttpUtils;

/**
 * 登录M层的实现类 by ASUS on 2018/4/23.
 */

public class LoginModelImpl implements ILoginModel {
    @Override
    public void showLoginJson(String url, Map map, final ILoginModelListener iLoginModelListener) {
        //在Model层进行网络请求
        HttpUtils httputils = HttpUtils.getHttputils();
        //使用POST方式进行请求
        httputils.postOkHttp(url, map);
        httputils.setFromHttpListener(new FromHttpListener() {
            @Override
            public void fromJosnSuccess(String json) {
                //请求成功
                //进行解析
                Gson gson = new Gson();
                LoginBean loginBean = gson.fromJson(json, LoginBean.class);
                String code = loginBean.getCode();
                if (code.equals("0")) {//判断当code等于0时为登录成功 否则为登录失败
                    iLoginModelListener.showLoginJsonSuccess(loginBean.getMsg());
                }else {
                    //请求失败  code不为0
                    iLoginModelListener.showLoginJsonError(loginBean.getMsg());
                }
            }

            @Override
            public void fromJsonError(String error) {
                //请求失败
                iLoginModelListener.showLoginJsonError(error);
            }
        });
    }
}
//注册实现类
//RegModelImpl
package zhaojunchao.bawie.com.zhaojunchao20180423.model;

import com.google.gson.Gson;

import java.util.Map;

import zhaojunchao.bawie.com.zhaojunchao20180423.utils.FromHttpListener;
import zhaojunchao.bawie.com.zhaojunchao20180423.utils.HttpUtils;

/**
 * 登录M层的实现类 by ASUS on 2018/4/23.
 */

public class RegModelImpl implements IRegModel {

    @Override
    public void showRegnJson(String url, Map map, final IRegModelListener iRegModelListener) {
        //在Model层进行网络请求
        HttpUtils httputils = HttpUtils.getHttputils();
        //使用POST方式进行请求
        httputils.postOkHttp(url, map);
        httputils.setFromHttpListener(new FromHttpListener() {
            @Override
            public void fromJosnSuccess(String json) {
                //请求成功
                //进行解析
                Gson gson = new Gson();
                RegBean regBean = gson.fromJson(json, RegBean.class);
                String code = regBean.getCode();
                if (code.equals("0")) {//判断当code等于0时为注册成功 否则为登录失败
                    iRegModelListener.showRegJsonSuccess(regBean.getMsg());
                }else {
                    //请求失败  code不为0
                    iRegModelListener.showRegJsonError(regBean.getMsg());
                }
            }

            @Override
            public void fromJsonError(String error) {
                //请求失败
                iRegModelListener.showRegJsonError(error);
            }
        });
    }
}
//Presenter
//IPresenter
package zhaojunchao.bawie.com.zhaojunchao20180423.presenter;

import zhaojunchao.bawie.com.zhaojunchao20180423.model.ILoginModel;
import zhaojunchao.bawie.com.zhaojunchao20180423.model.IRegModel;
import zhaojunchao.bawie.com.zhaojunchao20180423.view.ILoginView;
import zhaojunchao.bawie.com.zhaojunchao20180423.view.IRegView;

/**
 * P层 by ASUS on 2018/4/23.
 */

public interface IPresenter {
    //登录的回调方法
    void getLoginJson(ILoginModel iLoginModel, ILoginView iLoginView);
    //注册的回调方法
    void getRegJson(IRegModel iRegModel, IRegView iRegView);
}
//PresenterImpl
package zhaojunchao.bawie.com.zhaojunchao20180423.presenter;

import java.util.HashMap;
import java.util.Map;

import zhaojunchao.bawie.com.zhaojunchao20180423.http.HttpConfig;
import zhaojunchao.bawie.com.zhaojunchao20180423.model.ILoginModel;
import zhaojunchao.bawie.com.zhaojunchao20180423.model.ILoginModelListener;
import zhaojunchao.bawie.com.zhaojunchao20180423.model.IRegModel;
import zhaojunchao.bawie.com.zhaojunchao20180423.model.IRegModelListener;
import zhaojunchao.bawie.com.zhaojunchao20180423.view.ILoginView;
import zhaojunchao.bawie.com.zhaojunchao20180423.view.IRegView;

/**
 * P层的实现类 by ASUS on 2018/4/23.
 */

public class PresenterImpl implements IPresenter {
    //登录的回调方法
    @Override
    public void getLoginJson(ILoginModel iLoginModel, final ILoginView iLoginView) {
        //创建Map集合
        Map map = new HashMap<>();
        map.put("mobile", iLoginView.getMobile());
        map.put("password", iLoginView.getPassword());
        iLoginModel.showLoginJson(HttpConfig.login_url, map, new ILoginModelListener() {
            @Override
            public void showLoginJsonSuccess(String json) {
                //登录成功
                iLoginView.showLoginSuccess();
            }

            @Override
            public void showLoginJsonError(String error) {
                iLoginView.showLoginError(error);//登录失败
            }
        });
    }

    //注册
    @Override
    public void getRegJson(IRegModel iRegModel, final IRegView iRegView) {
        //创建Map集合
        Map map = new HashMap<>();
        map.put("mobile", iRegView.getMobile());
        map.put("password", iRegView.getPassword());
        iRegModel.showRegnJson(HttpConfig.reg_url, map, new IRegModelListener() {
            @Override
            public void showRegJsonSuccess(String json) {
                iRegView.showLoginSuccess();
            }

            @Override
            public void showRegJsonError(String error) {
                iRegView.showLoginError(error);
            }
        });
    }
}
//View
//ILoginView   登录的View
package zhaojunchao.bawie.com.zhaojunchao20180423.view;

/**
 * 登录的View层 by ASUS on 2018/4/23.
 */

public interface ILoginView {
    //登录成功的回调
    void showLoginSuccess();
    //登录失败的回调
    void showLoginError(String error);
    //获取输入的手机号
    String getMobile();
    //获取输入的密码
    String getPassword();
}
//IRegView    注册的View
package zhaojunchao.bawie.com.zhaojunchao20180423.view;

/**
 * 登录的View层 by ASUS on 2018/4/23.
 */

public interface IRegView {
    //登录成功的回调
    void showLoginSuccess();
    //登录失败的回调
    void showLoginError(String error);
    //获取输入的手机号
    String getMobile();
    //获取输入的密码
    String getPassword();
}
//MainActivity
package zhaojunchao.bawie.com.zhaojunchao20180423.view;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
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.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;
import java.util.Set;

import zhaojunchao.bawie.com.zhaojunchao20180423.R;
import zhaojunchao.bawie.com.zhaojunchao20180423.model.LoginModelImpl;
import zhaojunchao.bawie.com.zhaojunchao20180423.presenter.PresenterImpl;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, ILoginView {

    private EditText login_mobile;
    private EditText login_password;
    private Button login;
    private Button reg;
    private ImageView img;
    private TextView name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //手动权限
        if(Build.VERSION.SDK_INT>=23){
            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.CALL_PHONE,Manifest.permission.READ_LOGS,Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.SET_DEBUG_APP,Manifest.permission.SYSTEM_ALERT_WINDOW,Manifest.permission.GET_ACCOUNTS,Manifest.permission.WRITE_APN_SETTINGS};
            ActivityCompat.requestPermissions(this,mPermissionList,123);
        }
        //初始化数据
        initViews();

    }
    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
    }
    private void initViews() {
        //获取控件ID
        login_mobile = findViewById(R.id.login_mobile);
        login_password = findViewById(R.id.login_password);
        login = findViewById(R.id.login);
        img = findViewById(R.id.img);
        name = findViewById(R.id.name1);

        reg = findViewById(R.id.reg);
        login.setOnClickListener(this);
        reg.setOnClickListener(this);
        //登录按钮的长按事件
        login.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Toast.makeText(MainActivity.this, "你点击了长按事件", Toast.LENGTH_SHORT).show();
                UMShareAPI.get(MainActivity.this).getPlatformInfo(MainActivity.this, SHARE_MEDIA.QQ, authListener);
                return true;
            }
        });
    }

    //qq
    UMAuthListener authListener = new UMAuthListener() {
        /**
         * @desc 授权开始的回调
         * @param platform 平台名称
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {
        }
        /**
         * @desc 授权成功的回调
         * @param platform 平台名称
         * @param action 行为序号,开发者用不上
         * @param data 用户资料返回
         */
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map data) {
            Toast.makeText(MainActivity.this, "成功了", Toast.LENGTH_LONG).show();
            Set keySet = data.keySet();
            for (String key:keySet
                 ) {
                String value = data.get(key);
                name.setText(key);
                // 创建ImageLoader对象
                ImageLoader imageLoader = ImageLoader.getInstance();

            }
        }
        /**
         * @desc 授权失败的回调
         * @param platform 平台名称
         * @param action 行为序号,开发者用不上
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText(MainActivity.this, "失败:" + t.getMessage(),                                     Toast.LENGTH_LONG).show();
        }
        /**
         * @desc 授权取消的回调
         * @param platform 平台名称
         * @param action 行为序号,开发者用不上
         */
        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText(MainActivity.this, "取消了", Toast.LENGTH_LONG).show();
        }
    };

    //点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                //点击登陆
                PresenterImpl presenter = new PresenterImpl();
                presenter.getLoginJson(new LoginModelImpl(),MainActivity.this);
                break;
            case R.id.reg:
                //点击注册
                Intent intent =new Intent(MainActivity.this,RegActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void showLoginSuccess() {
        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(MainActivity.this,DetilActivity.class);
        startActivity(intent);
    }

    @Override
    public void showLoginError(String error) {
        Toast.makeText(this, "登录失败:"+error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public String getMobile() {
        return login_mobile.getText().toString();
    }

    @Override
    public String getPassword() {
        return login_password.getText().toString();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }
}
//RegActivity
package zhaojunchao.bawie.com.zhaojunchao20180423.view;

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

import zhaojunchao.bawie.com.zhaojunchao20180423.R;
import zhaojunchao.bawie.com.zhaojunchao20180423.model.RegModelImpl;
import zhaojunchao.bawie.com.zhaojunchao20180423.presenter.PresenterImpl;

public class RegActivity extends AppCompatActivity implements View.OnClickListener, IRegView {
    private EditText login_mobile;
    private EditText login_password;
    private Button login;
    private Button reg;
    private Button reg1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        initViews();
    }

    private void initViews() {
        //获取控件ID
        login_mobile = findViewById(R.id.login_mobile);
        login_password = findViewById(R.id.login_password);
        reg1 = findViewById(R.id.reg_but);
        reg1.setOnClickListener(this);
        
    }
    //点击事件
    @Override
    public void onClick(View v) {
        Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
        PresenterImpl presenter = new PresenterImpl();
        presenter.getRegJson(new RegModelImpl(),this);
    }

    @Override
    public void showLoginSuccess() {
        Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(RegActivity.this,DetilActivity.class);
        startActivity(intent);
    }

    @Override
    public void showLoginError(String error) {
        Toast.makeText(this, "注册失败"+error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public String getMobile() {
        return login_mobile.getText().toString();
    }

    @Override
    public String getPassword() {
        return login_password.getText().toString();
    }
}
//BeanUtils
//LoginBean
package zhaojunchao.bawie.com.zhaojunchao20180423.model;

/**
 * 登录的Bean by ASUS on 2018/4/23.
 */

public class LoginBean {

    /**
     * msg : 登录成功
     * code : 0
     * data : {"age":null,"appkey":"1da9c47d431c043b","appsecret":"A804850E463E7E282E09E1AD112D9808","createtime":"2018-04-23T09:41:36","email":null,"fans":null,"follow":null,"gender":null,"icon":null,"latitude":null,"longitude":null,"mobile":"18611112222","money":null,"nickname":null,"password":"8F669074CAF5513351A2DE5CC22AC04C","praiseNum":null,"token":"A4A74B21D1D9E47AA217D6CDAB9A0CE5","uid":11196,"userId":null,"username":"18611112222"}
     */

    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 : 1da9c47d431c043b
         * appsecret : A804850E463E7E282E09E1AD112D9808
         * createtime : 2018-04-23T09:41:36
         * email : null
         * fans : null
         * follow : null
         * gender : null
         * icon : null
         * latitude : null
         * longitude : null
         * mobile : 18611112222
         * money : null
         * nickname : null
         * password : 8F669074CAF5513351A2DE5CC22AC04C
         * praiseNum : null
         * token : A4A74B21D1D9E47AA217D6CDAB9A0CE5
         * uid : 11196
         * userId : null
         * username : 18611112222
         */

        private Object age;
        private String appkey;
        private String appsecret;
        private String createtime;
        private Object email;
        private Object fans;
        private Object follow;
        private Object gender;
        private Object icon;
        private Object latitude;
        private Object longitude;
        private String mobile;
        private Object money;
        private Object nickname;
        private String password;
        private Object praiseNum;
        private String token;
        private int uid;
        private Object userId;
        private String username;

        public Object getAge() {
            return age;
        }

        public void setAge(Object age) {
            this.age = age;
        }

        public String getAppkey() {
            return appkey;
        }

        public void setAppkey(String appkey) {
            this.appkey = appkey;
        }

        public String getAppsecret() {
            return appsecret;
        }

        public void setAppsecret(String 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 Object getFans() {
            return fans;
        }

        public void setFans(Object fans) {
            this.fans = fans;
        }

        public Object getFollow() {
            return follow;
        }

        public void setFollow(Object follow) {
            this.follow = follow;
        }

        public Object getGender() {
            return gender;
        }

        public void setGender(Object gender) {
            this.gender = gender;
        }

        public Object getIcon() {
            return icon;
        }

        public void setIcon(Object icon) {
            this.icon = icon;
        }

        public Object getLatitude() {
            return latitude;
        }

        public void setLatitude(Object latitude) {
            this.latitude = latitude;
        }

        public Object getLongitude() {
            return longitude;
        }

        public void setLongitude(Object longitude) {
            this.longitude = longitude;
        }

        public String getMobile() {
            return mobile;
        }

        public void setMobile(String mobile) {
            this.mobile = mobile;
        }

        public Object getMoney() {
            return money;
        }

        public void setMoney(Object 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 getPraiseNum() {
            return praiseNum;
        }

        public void setPraiseNum(Object praiseNum) {
            this.praiseNum = praiseNum;
        }

        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token;
        }

        public int getUid() {
            return uid;
        }

        public void setUid(int uid) {
            this.uid = uid;
        }

        public Object getUserId() {
            return userId;
        }

        public void setUserId(Object userId) {
            this.userId = userId;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }
    }
}
//RegBean
package zhaojunchao.bawie.com.zhaojunchao20180423.model;

/**
 * 注册的Bean by ASUS on 2018/4/23.
 */

public class RegBean {

    /**
     * msg : 天呢!用户名或密码不能为空
     * code : 1
     * data : {}
     */

    private String msg;
    private String code;
    private String 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 String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}




















你可能感兴趣的:(mvp+okhttp注册登录)