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); }注册接口RegListenerpackage 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); } }