安卓登录注册---model层

Imodel接口
package com.xinzhengwei.denglu.model;

import com.xinzhengwei.denglu.model.LodinListener;
import com.xinzhengwei.denglu.model.RegListener;
import com.xinzhengwei.denglu.model.User;

/**
 * Created by 辛政维 on 2018/1/12.
 */

public interface Imodel {

    void login(User user,LodinListener lodinListener);
    void reg(User user,RegListener regListener);

}

LodinListener登录接口
package com.xinzhengwei.denglu.model;

/**
 * Created by 辛政维 on 2018/1/12.
 */

public interface LodinListener {

    void lodinSucess(String json);
    void lodinError(String erroe);

}

注册接口RegListener
package com.xinzhengwei.denglu.model;

/**
 * Created by 辛政维 on 2018/1/12.
 */

public interface RegListener {

        void RegSucess();
        void RegError();

}
 
OkHttpLoadListener接口

package com.xinzhengwei.denglu.model;

/**
 * Created by 辛政维 on 2018/1/12.
 */

public interface OkHttpLoadListener {

        void loadSucess(String json);
        void loadErroe(String erroe);

}

封装一个数据信息类
package com.xinzhengwei.denglu.model;

/**
 * Created by 辛政维 on 2018/1/12.
 */

public class User {

    private String mobile;
    private String password;

    public User(String mobile, String password) {
        this.mobile = mobile;
        this.password = password;
    }

    public String getMobile() {
        return mobile;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "mobile='" + mobile + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

IModel实现类
package com.xinzhengwei.denglu.model;

import com.xinzhengwei.denglu.Utils.HttpConig;
import com.xinzhengwei.denglu.Utils.OkHttp;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 辛政维 on 2018/1/12.
 */

public class ImodelImp implements Imodel {
    @Override
    public void login(User user, final LodinListener lodinListener) {

        String mobile = user.getMobile();
        String password = user.getPassword();

        OkHttp okHttp = new OkHttp();
        okHttp.setOkHttpLoadListener(new OkHttpLoadListener() {
            @Override
            public void loadSucess(String json) {

                try {
                    JSONObject jsonObject = new JSONObject(json);
                    String code = jsonObject.getString("code");
                    if (code.equals("0")){
                        lodinListener.lodinSucess(json);
                    }else {
                        lodinListener.lodinError("失败");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void loadErroe(String erroe) {
                    lodinListener.lodinError(erroe);
            }
        });
        okHttp.okHttpUtils(HttpConig.login_url+"?mobile="+mobile+"&password="+password);
    }

    @Override
    public void reg(User user, final RegListener regListener) {

        String mobile = user.getMobile();
        String password = user.getPassword();

        OkHttp okHttp = new OkHttp();
        okHttp.setOkHttpLoadListener(new OkHttpLoadListener() {
            @Override
            public void loadSucess(String json) {
                try {
                    JSONObject object = new JSONObject(json);
                    String code = object.getString("code");
                    if (code.equals("0")){
                        regListener.RegSucess();
                    }else {
                        regListener.RegError();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void loadErroe(String erroe) {
                regListener.RegError();
            }
        });
        okHttp.okHttpUtils(HttpConig.reg_url+"?mobile="+mobile+"&password="+password);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值