api接口
@POST("/user/loginjson") Observable<UserEntity> postLogin(@Body RequestBody requestBody); @POST("/user/register") Observable<UserEntity> postRegister(@Body RequestBody requestBody); @GET("/video/findVideos?currentPage=10&pageSize=10") Observable<VideoEntity> getVideoData();
model
protected Api api; public Model() { api = RetrofitManager.getRetrofitManager().getRetrofit().create(Api.class); } public Observable<UserEntity> postLogin(RequestBody requestBody){ return api.postLogin(requestBody); } public Observable<UserEntity> postRegister(RequestBody requestBody){ return api.postRegister(requestBody); } public Observable<VideoEntity> getVideoData(){ return api.getVideoData(); }
viewModel
private Model model; public MutableLiveData<UserEntity> loginLiveData = new MutableLiveData<>(); public MutableLiveData<UserEntity> registerLiveData = new MutableLiveData<>(); public MutableLiveData<VideoEntity> videoLiveData = new MutableLiveData<>(); public ViewModel() { model = new Model(); } public void login(RequestBody requestBody){ model.postLogin(requestBody) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<UserEntity>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(UserEntity userEntity) { Log.i("TagA",userEntity.getCode()+""); loginLiveData.postValue(userEntity); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } public void register(RequestBody requestBody){ model.postRegister(requestBody) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<UserEntity>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(UserEntity userEntity) { registerLiveData.postValue(userEntity); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } public void getVideo(){ model.getVideoData() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<VideoEntity>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(VideoEntity videoEntity) { videoLiveData.postValue(videoEntity); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); }
activity
ActivityLoginBinding loginBinding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); loginBinding = DataBindingUtil.setContentView(this,R.layout.activity_login); setContentView(loginBinding.getRoot()); ViewModel viewModel = new ViewModelProvider(this).get(ViewModel.class); loginBinding.loginBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String username = loginBinding.account.getText().toString().trim(); String password = loginBinding.password.getText().toString().trim(); Map<String,String> map = new HashMap<>(); map.put("username",username); map.put("password",password); String json = new Gson().toJson(map); RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"),json); viewModel.login(requestBody); viewModel.loginLiveData.observe(LoginActivity.this, new Observer<UserEntity>() { @Override public void onChanged(UserEntity userEntity) { if (userEntity.getCode() == 200){ Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show(); Log.i("TagA","登录成功"); Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); }else { Toast.makeText(LoginActivity.this, "登录失败:"+userEntity.getMessage(), Toast.LENGTH_SHORT).show(); Log.i("TagA","登录失败"); } } }); } }); loginBinding.toRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(LoginActivity.this, RegisterActivity.class); startActivity(intent); } }); }
ActivityRegisterBinding registerBinding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); registerBinding = DataBindingUtil.setContentView(this,R.layout.activity_register); setContentView(registerBinding.getRoot()); ViewModel viewModel = new ViewModelProvider(this).get(ViewModel.class); registerBinding.loginBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String username = registerBinding.account.getText().toString().trim(); String password = registerBinding.password.getText().toString().trim(); String repassword = registerBinding.repassword.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); viewModel.register(requestBody); viewModel.registerLiveData.observe(RegisterActivity.this, new Observer<UserEntity>() { @Override public void onChanged(UserEntity userEntity) { if (userEntity.getCode() == 200){ Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show(); }else { Toast.makeText(RegisterActivity.this, "注册失败:"+userEntity.getMessage(), Toast.LENGTH_SHORT).show(); } } }); } }); }
ActivityMainBinding mainBinding; private VideoAdapter videoAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main); setContentView(mainBinding.getRoot()); ViewModel viewModel = new ViewModelProvider(this).get(ViewModel.class); viewModel.getVideo(); viewModel.videoLiveData.observe(this, new Observer<VideoEntity>() { @Override public void onChanged(VideoEntity videoEntity) { videoAdapter = new VideoAdapter(R.layout.item_video,videoEntity.getData()); videoAdapter.notifyDataSetChanged(); mainBinding.videoRcv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)); mainBinding.videoRcv.setAdapter(videoAdapter); } }); }