1.http
RetrofitManager
public class RetrofitManager { protected static RetrofitManager retrofitManager=null; public RetrofitManager() { } public static RetrofitManager getInstance() { if(retrofitManager==null){ retrofitManager=new RetrofitManager(); } return retrofitManager; } private Retrofit retrofit; public Retrofit getRetrofit() { if(retrofit==null){ retrofit=createRetrofit(); } return retrofit; } private Retrofit createRetrofit() { OkHttpClient client=new OkHttpClient.Builder() .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) .build(); retrofit=new Retrofit.Builder() .client(client) .baseUrl("http://10.161.9.80:7012") .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); return retrofit; } }
Api(接口)
public interface Api { @GET("foods/getFoods?") Observable<FoodEntity> getFoodData(@Query("currentPage")int currentPage,@Query("pageSize") int pageSize); @POST("/user/register") Observable<UserEntity> postregister(@Body RequestBody body); @POST("/user/loginjson") Observable<UserEntity> postLoginData(@Body RequestBody body); }
2.model(BaseModel)
public class BaseModel { protected Api api; public BaseModel() { api= RetrofitManager.getInstance().getRetrofit().create(Api.class); } }
3.viewmodel(BaseViewModel)
public abstract class BaseViewModel<BaMo extends BaseModel> extends ViewModel { protected BaMo mModel; public BaseViewModel() { mModel=createModel(); } protected abstract BaMo createModel(); }
4.view(BaseActivity)
public abstract class BaseActivity<VDB extends ViewDataBinding,VM extends BaseViewModel> extends AppCompatActivity { protected VDB viewDataBind; protected VM viewmodel; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); viewDataBind= DataBindingUtil.setContentView(this,getLayoutid()); viewmodel=new ViewModelProvider(this).get(getViewModel()); initView(); initData(); handObserver(); } protected void handObserver() { } protected abstract void initData(); protected abstract void initView(); protected abstract Class<VM> getViewModel(); protected abstract int getLayoutid();
}
.mvvm(login)
5.(LoginModel)
public class LoginModel extends BaseModel { public Observable<UserEntity> postloginDATA(RequestBody body){ return api.postLoginData(body); } }
6.(LoginViewModel)
public class LoginViewModel extends BaseViewModel<LoginModel> { public MutableLiveData<UserEntity> loginLiveData=new MutableLiveData<>(); @Override protected LoginModel createModel() { return new LoginModel(); } public void postloginData(RequestBody body){ mModel.postloginDATA(body) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<UserEntity>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(UserEntity userEntity) { loginLiveData.postValue(userEntity); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); }
}
7.(LoginActivity)
public class LoginActivity extends BaseActivity<ActivityLoginBinding, LoginViewModel> { @Override protected void initData() { } @Override protected void initView() { viewDataBind.login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String username=viewDataBind.username.getText().toString().trim(); String password=viewDataBind.password.getText().toString().trim(); Map<String,String> map=new HashMap<>(); map.put("username",username); map.put("password",password); //将map集合转换为json String json=new Gson().toJson(map); RequestBody requestBody=RequestBody.create(MediaType.parse("application/json;charset=utf-8"),json); //启动mvvm viewmodel.postloginData(requestBody); } }); } @Override protected Class<LoginViewModel> getViewModel() { return LoginViewModel.class; } @Override protected int getLayoutid() { return R.layout.activity_login; } @Override protected void handObserver() { super.handObserver(); viewmodel.loginLiveData.observe(this, new Observer<UserEntity>() { @Override public void onChanged(UserEntity userEntity) { if(userEntity.getCode()==200){ Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); }else{ Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show(); } } }); }
.mvvm(register)
8.(registerModel)
public class RegisterModel extends BaseModel { public Observable<UserEntity> postRegisterData(RequestBody body){ return api.postregister(body); } }
9.(RegisterViewModel)
public class RegisterViewModel extends BaseViewModel<RegisterModel> { public MutableLiveData<UserEntity> userEntityMutableLiveData=new MutableLiveData<>(); @Override protected RegisterModel createModel() { return new RegisterModel(); } public void postRegister(RequestBody body){ mModel.postRegisterData(body) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(new Observer<UserEntity>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(UserEntity userEntity) { userEntityMutableLiveData.postValue(userEntity); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } }
10.(RegisterActivity)
public class RegisterActivity extends BaseActivity<ActivityRegisterBinding,RegisterViewModel> { @Override protected void initData() { } @Override protected void initView() { viewDataBind.btnRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String username=viewDataBind.etUser.getText().toString().trim(); String password=viewDataBind.etPassword.getText().toString().trim(); String repassword=viewDataBind.etRepassword.getText().toString().trim(); Map<String,String> map=new HashMap<>(); map.put("username",username); map.put("password",password); map.put("repassword",repassword); String json=new Gson().toJson(map); RequestBody requestBody=RequestBody.create(MediaType.parse("application/json;charset=utf-8"),json); //启动mvvm viewmodel.postRegister(requestBody); } }); } @Override protected Class<RegisterViewModel> getViewModel() { return RegisterViewModel.class; } @Override protected int getLayoutid() { return R.layout.activity_register; } @Override protected void handObserver(){ super.handObserver(); viewmodel.userEntityMutableLiveData.observe(this, new Observer<UserEntity>() { @Override public void onChanged(UserEntity userEntity) { if(userEntity.getCode()==200){ Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(RegisterActivity.this, LoginActivity.class); startActivity(intent); }else{ Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show(); } } }); }
}