//所需要的依赖包:
compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.squareup.okhttp3:logging-interceptor:3.9.0' compile 'com.squareup.retrofit2:retrofit:2.0.2' compile 'com.squareup.retrofit2:converter-gson:2.0.2' compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0' compile 'io.reactivex:rxandroid:1.0.1'
main_activity:
register_activity:android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="10dp"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="京东登录" android:textSize="25sp" /> android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingLeft="20dp" android:paddingRight="20dp"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账号" android:textSize="20sp" /> android:id="@+id/accont" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入账号" /> android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingLeft="20dp" android:paddingRight="20dp"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码" android:textSize="20sp" /> android:id="@+id/pass" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="numberPassword" android:hint="请输入密码" /> android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="20dp"> android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="20dp"> android:id="@+id/regist" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请注册" />
Api:android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="10dp"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="京东注册" android:textSize="25sp" /> android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingLeft="20dp" android:paddingRight="20dp"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账号" android:textSize="20sp" /> android:id="@+id/raccont" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入账号" /> android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingLeft="20dp" android:paddingRight="20dp"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码" android:textSize="20sp" /> android:id="@+id/rpass" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" /> android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="20dp">
public class Api { public static final String HOST = "https://www.zhaoapi.cn/"; public static final String LOGIN = "user/login"; public static final String REGIST = "user/reg"; }回调方法:
public interface OnNetListener<T> { public void onSuccess(T t); public void onFailure(Exception e); }ServerApi接口:
public interface ServerApi { @GET(Api.LOGIN) ObservableRetrofitHelper:login(@Query("mobile") String phone, @Query("password") String pass);//登录 @GET(Api.REGIST) Observable regist(@Query("mobile") String phone, @Query("password") String pass);//注册 }
public class RetrofitHelper { private static OkHttpClient client; private static ServerApi serverApi; static { getClient(); } public static OkHttpClient getClient() { if (client == null) { synchronized (OkHttpClient.class){ if (client == null){ HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.getLevel(); client = new OkHttpClient.Builder() .addInterceptor(logging) .build(); } } } return client; } public static ServerApi getServerApi() { if (serverApi == null) { synchronized (ServerApi.class){ if (serverApi == null){ serverApi = onCreate(ServerApi.class,Api.HOST); } } } return serverApi; } public static <T> T onCreate(Class<T> tClass, String url){ Retrofit retrofit = new Retrofit.Builder() .baseUrl(url) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); return retrofit.create(tClass); } }ILoginModel接口类:
public interface ILoginModel { public void getLogin(final OnNetListenerIRegisterModel的接口类:onNetListener, String phone, String pass); }
public interface IRegisterModel { public void getRegister(final OnNetListenerLoginMode:onNetListener, String phone, String pass); }
public class LoginModel implements ILoginModel { @Override public void getLogin(final OnNetListenerRegisterModel:onNetListener, String phone, String pass) { ServerApi serverApi = RetrofitHelper.getServerApi(); serverApi.login(phone,pass) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber () { @Override public void onCompleted() { } @Override public void onError(Throwable e) { onNetListener.onFailure((Exception) e); } @Override public void onNext(LoginBean loginBean) { onNetListener.onSuccess(loginBean); } }); } }
public class RegisterModel implements IRegisterModel { @Override public void getRegister(final OnNetListenerLoginPresenter:onNetListener, String phone, String pass) { ServerApi serverApi = RetrofitHelper.getServerApi(); serverApi.regist(phone,pass) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber () { @Override public void onCompleted() { } @Override public void onError(Throwable e) { onNetListener.onFailure((Exception) e); } @Override public void onNext(RegisterBean registerBean) { onNetListener.onSuccess(registerBean); } }); } }
public class LoginPresenter { private ILoginModel iLoginModel; private ILoginActivity iLoginActivity; public LoginPresenter(ILoginActivity iLoginActivity) { this.iLoginActivity = iLoginActivity; iLoginModel = new LoginModel(); } public void setLogin(String phone,String pass){ iLoginModel.getLogin(new OnNetListenerRegisterPresenter:() { @Override public void onSuccess(LoginBean loginBean) { String code = loginBean.getCode(); String msg = loginBean.getMsg(); iLoginActivity.showLogin(loginBean,code,msg); } @Override public void onFailure(Exception e) { } },phone,pass); } }
public class RegisterPresenter { private IRegisterModel iRegisterModel; private IRegisterActivity iRegisterActivity; public RegisterPresenter(IRegisterActivity iRegisterActivity) { this.iRegisterActivity = iRegisterActivity; iRegisterModel = new RegisterModel(); } public void setRegist(String phone,String pass){ iRegisterModel.getRegister(new OnNetListenerILoginActivity:() { @Override public void onSuccess(RegisterBean registerBean) { String code = registerBean.getCode(); String msg = registerBean.getMsg(); iRegisterActivity.showRegister(code,msg); } @Override public void onFailure(Exception e) { } }, phone, pass); } }
public interface ILoginActivity { public void showLogin(LoginBean loginBean, String code, String msg); }IRegisterActivity:
public interface IRegisterActivity { public void showRegister(String code,String msg); }mainActivity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener, ILoginActivity { /** * 请输入账号 */ private EditText mAccont; /** * 请输入密码 */ private EditText mPass; /** * 登录 */ private Button mLogin; /** * 请注册 */ private TextView mRegist; private LoginPresenter loginPresenter; String codes; String msgs; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); ActionBar bar = getSupportActionBar(); bar.hide(); loginPresenter = new LoginPresenter(this); } private void initView() { mAccont = (EditText) findViewById(R.id.accont); mPass = (EditText) findViewById(R.id.pass); mLogin = (Button) findViewById(R.id.login); mLogin.setOnClickListener(this); mRegist = (TextView) findViewById(R.id.regist); mRegist.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { default: break; case R.id.login: String phone = mAccont.getText().toString().trim(); String pass = mPass.getText().toString().trim(); loginPresenter.setLogin(phone, pass); break; case R.id.regist: Intent intent1 = new Intent(this, RegisterActivity.class); startActivity(intent1); break; } } @Override public void showLogin(LoginBean loginBean, String code, String msg) { codes = code; msgs = msg; if (codes.equals("0")) { Toast.makeText(this, msgs, Toast.LENGTH_SHORT).show(); LoginBean.DataBean data = loginBean.getData(); int uid = data.getUid(); String name = data.getUsername(); String token = data.getToken(); MyApp.edit.putInt("uid", uid); MyApp.edit.putString("name", name); MyApp.edit.putString("token", token); MyApp.edit.putBoolean("flag", true); MyApp.edit.commit();
Intent intent = new Intent(this, ShowActivity.class); intent.putExtra("frag", 5); startActivity(intent);finish() ; } else { MyApp. edit.putBoolean( "flag" , false) ; MyApp. edit.commit() ; Toast. makeText( this, msgs , Toast. LENGTH_SHORT).show() ; } }} RegisterActivity:
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener,IRegisterActivity { /** * 请输入账号 */ private EditText mRaccont; /** * 请输入密码 */ private EditText mRpass; /** * 注册 */ private Button mRegister; private RegisterPresenter registerPresenter; String codes; String msgs; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); initView(); ActionBar bar = getSupportActionBar(); bar.hide(); registerPresenter = new RegisterPresenter(this); } private void initView() { mRaccont = (EditText) findViewById(R.id.raccont); mRpass = (EditText) findViewById(R.id.rpass); mRegister = (Button) findViewById(R.id.register); mRegister.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { default: break; case R.id.register: String phone = mRaccont.getText().toString().trim(); String pass = mRpass.getText().toString().trim(); registerPresenter.setRegist(phone,pass); break; } } @Override public void showRegister(String code, String msg) { codes = code; msgs = msg; if (codes.equals("0")){ Toast.makeText(this,msgs,Toast.LENGTH_SHORT).show(); if (msgs.equals("注册成功")){ Intent intent = new Intent(this, MainActivity.class); startActivity(intent); finish(); } } else { Toast.makeText(this,msgs,Toast.LENGTH_SHORT).show(); } } }MyApp:
public class MyApp extends Application { public static SharedPreferences sp; public static SharedPreferences.Editor edit; @Override public void onCreate() { super.onCreate(); sp = getSharedPreferences("user", MODE_PRIVATE); edit = sp.edit(); } }