ApiService代码-----------
/** * 注册 * @param mobile * @param password * @return */ @GET("user/reg") Flowable<ResigterBean> getRegService(@Query("mobile") String mobile,@Query("password") String password); /** * 登录 * @param mobile * @param password * @return */ @GET("user/login") Flowable<LoginBean> getLogService(@Query("mobile") String mobile,@Query("password") String password);BaseUrl代码---
//登录和注册和共用
public static final String REG_URL="http://120.27.23.105/";
注册页面布局----------
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#EDEEEE"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="20dp" android:background="#FF7802" > <TextView android:layout_width="0dp" android:layout_weight="0.5" android:layout_height="wrap_content" android:background="@drawable/left" android:id="@+id/fan"/> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_marginLeft="20dp" android:layout_height="wrap_content" android:text="新用户注册" android:textSize="20sp"/> </LinearLayout> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="20dp" android:id="@+id/ename" android:hint="请输入用户名"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="20dp" android:id="@+id/epass" android:hint="请输入密码"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="20dp" android:hint="确认输入密码" android:id="@+id/qepass"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="20dp" android:id="@+id/email" android:hint="请填写邮箱"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="注册" android:background="@drawable/but_shape" android:layout_marginTop="20dp" android:id="@+id/but_zc"/> </LinearLayout>注册页面代码------------
package com.example.com.moni; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.example.com.moni.bean.ResigterBean; import com.example.com.moni.presenter.RegPresenter; import com.example.com.moni.view.IRegView; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; public class ResgiterActivity extends AppCompatActivity implements IRegView{ @BindView(R.id.ename) EditText ename; @BindView(R.id.epass) EditText epass; @BindView(R.id.qepass) EditText qepass; @BindView(R.id.email) EditText email; @BindView(R.id.fan) TextView fan; @BindView(R.id.but_zc) Button butZc; private String msg; private String e_name; private String e_pass; private RegPresenter regPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_resgiter); ButterKnife.bind(this); //擦黄建p层数据方法 regPresenter = new RegPresenter(this); } //请求数据方法 @Override public void getRegData(ResigterBean resigterBean) { msg = resigterBean.getMsg(); //判断 if(msg.equals("注册成功")){ Intent intent1=new Intent(ResgiterActivity.this,MainActivity.class); startActivity(intent1); }else{ Toast.makeText(ResgiterActivity.this,msg,Toast.LENGTH_SHORT).show(); } } //按钮点击事件 @OnClick({R.id.fan, R.id.but_zc}) public void onViewClicked(View view) { //注册页面返回 switch (view.getId()) { case R.id.fan: Intent intent=new Intent(ResgiterActivity.this,MainActivity.class); startActivity(intent); break; case R.id.but_zc: e_name= ename.getText().toString(); e_pass = epass.getText().toString(); regPresenter.getRMdata(e_name,e_pass); break; } } }
注册页面mode层
public class LogModel { public void getLogData(IlogPresenter ilogPresenter, String mobile, String password) { //带哦用工具诶方法 ApiService apiService = HttpUtils.getInstance(BaseUrl.REG_URL).getApiService(); Flowable<LoginBean> logService = apiService.getLogService(mobile, password); //把得到数据传给p ilogPresenter.success(logService); } }
注册页面presenter层接口
public interface IRegPresenter { //定义成功方法 void success(Flowable<ResigterBean> regService); }
注册页面presenter层类
package com.example.com.moni.presenter; import com.example.com.moni.bean.ResigterBean; import com.example.com.moni.model.RegModdel; import com.example.com.moni.view.IRegView; import io.reactivex.Flowable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import io.reactivex.subscribers.DisposableSubscriber; /** * Created by len on 2018/4/29. */ public class RegPresenter implements IRegPresenter{ private final IRegView iregView; //提供有参构造 public RegPresenter(IRegView iregView){ this.iregView=iregView; } //p层回调方法 public void getRMdata(String mobile, String password) { //创建m层对象 RegModdel regModdel=new RegModdel(); regModdel.getData(this,mobile,password); } @Override public void success(Flowable<ResigterBean> regService) { regService.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new DisposableSubscriber<ResigterBean>() { @Override public void onNext(ResigterBean resigterBean) { //把解析的数据传给view层 iregView.getRegData(resigterBean); } @Override public void onError(Throwable t) { } @Override public void onComplete() { } }); } }注册页面view层接口
package com.example.com.moni.view; import com.example.com.moni.bean.ResigterBean; /** * Created by len on 2018/4/29. */ public interface IRegView { //定义得到数据方法 void getRegData(ResigterBean resigterBean); }
登录页面布局------------------------------------------
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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.example.com.moni.MainActivity" android:orientation="vertical" android:background="#EDEEEE" android:layout_gravity="center_horizontal" android:gravity="center_horizontal"> <ImageView android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/tiaobao" android:layout_marginTop="20dp" android:src="@drawable/tiaobao" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/ed_name" android:hint="手机号/会员号/邮箱" android:padding="20dp"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/ed_pass" android:hint="请输入密码" android:padding="20dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="20dp" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="忘记密码"/> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="新用户注册" android:id="@+id/register" android:layout_marginLeft="200dp"/> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" android:background="@drawable/but_shape" android:layout_marginTop="10dp" android:id="@+id/deng"/> <ImageView android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/qq" android:layout_marginTop="20dp" android:id="@+id/qq_login"/> </LinearLayout>
登录页面的类-----------
package com.example.com.moni; import android.content.Intent; import android.content.SharedPreferences; 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.TextView; import android.widget.Toast; import com.example.com.moni.bean.LoginBean; import com.example.com.moni.presenter.LogPresenter; import com.example.com.moni.view.ILogView; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; public class MainActivity extends AppCompatActivity implements ILogView{ @BindView(R.id.tiaobao) ImageView tiaobao; @BindView(R.id.ed_name) EditText edName; @BindView(R.id.ed_pass) EditText edPass; @BindView(R.id.register) TextView register; @BindView(R.id.deng) Button deng; @BindView(R.id.qq_login) ImageView qqLogin; private String e_name; private String e_pass; private LogPresenter logPresenter; private SharedPreferences.Editor edit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); //创建p层对象 logPresenter = new LogPresenter(this); //得到getSharedPreferences SharedPreferences shop = getSharedPreferences("shop", MODE_PRIVATE); edit = shop.edit(); } //view层请求数方法 @Override public void getLogData(LoginBean loginBean) { //得到请求数据 String msg = loginBean.getMsg(); //判断 if(msg.equals("登录成功")){ //存入登录时的UID int uid = loginBean.getData().getUid(); edit.putInt("uid",uid); edit.commit(); //跳转到首页 Intent intent=new Intent(MainActivity.this,HaredActivity.class); startActivity(intent); }else{ Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show(); } } @OnClick({R.id.register, R.id.deng, R.id.qq_login}) public void onViewClicked(View view) { switch (view.getId()) { //新用户注册 case R.id.register: //跳转到注册页面 Intent intent=new Intent(MainActivity.this,ResgiterActivity.class); startActivity(intent); break; case R.id.deng: //得到输入框name pass e_name = edName.getText().toString(); e_pass = edPass.getText().toString(); //调用p层回调方法 logPresenter.getLogMdata(e_name,e_pass); // Intent intent1=new Intent(MainActivity.this,HaredActivity.class); // startActivity(intent1); break; case R.id.qq_login: break; } } }
登录model层类
package com.example.com.moni.model; import com.example.com.moni.api.ApiService; import com.example.com.moni.api.BaseUrl; import com.example.com.moni.bean.LoginBean; import com.example.com.moni.presenter.IlogPresenter; import com.example.com.moni.presenter.LogPresenter; import com.example.com.moni.utils.HttpUtils; import io.reactivex.Flowable; /** * Created by len on 2018/4/29. */ public class LogModel { public void getLogData(IlogPresenter ilogPresenter, String mobile, String password) { //带哦用工具诶方法 ApiService apiService = HttpUtils.getInstance(BaseUrl.REG_URL).getApiService(); Flowable<LoginBean> logService = apiService.getLogService(mobile, password); //把得到数据传给p ilogPresenter.success(logService); } }
登录页面p层接口
package com.example.com.moni.presenter; import com.example.com.moni.bean.LoginBean; import io.reactivex.Flowable; /** * Created by len on 2018/4/29. */ public interface IlogPresenter { //定义成功方法 void success(Flowable<LoginBean> logService); }
登录页面p层类
package com.example.com.moni.presenter; import com.example.com.moni.bean.LoginBean; import com.example.com.moni.model.LogModel; import com.example.com.moni.view.ILogView; import io.reactivex.Flowable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import io.reactivex.subscribers.DisposableSubscriber; /** * Created by len on 2018/4/29. */ public class LogPresenter implements IlogPresenter{ private ILogView iLogView; //提供有参构造 public LogPresenter(ILogView iLogView){ this.iLogView=iLogView; } //销毁view public void dettach(){ if(iLogView!=null){ iLogView=null; } } //定义p层回调方法 public void getLogMdata(String mobile, String password){ LogModel logModel=new LogModel(); logModel.getLogData(this,mobile,password); } @Override public void success(Flowable<LoginBean> logService) { logService.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new DisposableSubscriber<LoginBean>() { @Override public void onNext(LoginBean loginBean) { //传给p iLogView.getLogData(loginBean); } @Override public void onError(Throwable t) { } @Override public void onComplete() { } }); } }
登录view层接口
package com.example.com.moni.view; import com.example.com.moni.bean.LoginBean; /** * Created by len on 2018/4/29. */ public interface ILogView { //得到数据方法 void getLogData(LoginBean loginBean); }