版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhaihaohao1/article/details/83446969
思路:
1请求数据,根据用户名和密码,获取User_id
2再请求数据,根据User_id,获取用户信息
具体实现代码:
2.1、在build.gradle中添加依赖
compile 'com.jakewharton.rxbinding:rxbinding:0.4.0'
这个依赖包中包含了
rxbinding-0.4.0 (操作控件)
rxjava-1.1.0 (Java相关的)
rxandroid-1.1.0 (安卓相关的)
2.2、MainActivity中
package com.zhh.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.orhanobut.logger.Logger;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
/**
* 第四节
* 登录接口返回的用户信息
* 思路:
* 1请求数据,根据用户名和密码,获取User_id
* 2在请求数据,根据User_id,获取用户信息
*
* FlatMap将一个发射数据的Observable变换为多个Observables,然后将它们发射的数据合并后放进一个单独的Observable
*/
public class MainActivity extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickMap();
}
});
}
/**
* 登录接口需要的参数
*
* @return
*/
private UserParam getUserParam() {
UserParam pa = new UserParam("cniao5", "123456");
return pa;
}
/**
* 登录接口返回的setUser_id
* 作为下一个接口的参数
*
* @return
*/
private BaseResult getBaseResult() {
BaseResult baseResult = new BaseResult();
baseResult.setUser_id(112);
return baseResult;
}
/**
* @return 登录接口返回的用户信息
*/
private User getUser() {
User user = new User();
user.setUsername("zhh");
return user;
}
private void clickMap() {
// 发送 UserParam对象
Observable.just(getUserParam())
// 传入 UserParam 对象,返回 BaseResult 对象
.flatMap(new Func1<UserParam, Observable<BaseResult>>() {
@Override
public Observable<BaseResult> call(UserParam userParam) {
// 模拟数据请求
// 数据请求
String userName = userParam.getUsername();
String passWord = userParam.getPassword();
// 请求数据,然后把返回的数据分成 UserParam 对象
BaseResult baseResult = getBaseResult();
// 发送BaseResult
return Observable.just(baseResult);
}
})
// 传入BaseResult对象,返回User对象
.flatMap(new Func1<BaseResult, Observable<User>>() {
@Override
public Observable<User> call(BaseResult baseResult) {
int id = baseResult.getUser_id();
// 模拟数据请求
// id作为参数去请求数据,返回的数据,封装成User对象
User user = getUser();
// 发送 user
return Observable.just(user);
}
})
// 被观察者,设置为子线程
.subscribeOn(Schedulers.io())
// 观察者,设置为主线程
.observeOn(AndroidSchedulers.mainThread())
// 订阅,接收user对象
.subscribe(new Observer<User>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(User user) {
// 接收user对象
Logger.t("111").d("登录后获取用户信息" + user.getUsername());
}
});
}
}
2.3、实体类 UserParam
package com.zhh.app;
/**
* Created by 16838 on 2018/10/25.
*/
public class UserParam {
private String username;
private String password;
public UserParam(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2.4、实体类BaseResult
package com.zhh.app;
/**
* Created by 16838 on 2018/10/25.
*/
public class BaseResult {
private int success;
private String message;
private int user_id;
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public int getSuccess() {
return success;
}
public void setSuccess(int success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
2.5、实体类 User
package com.zhh.app;
/**
* Created by Ivan on 2016/10/7.
*/
public class User {
private String head_url;
private String id;
private String username;
public User(String head_url, String id, String username) {
this.head_url = head_url;
this.id = id;
this.username = username;
}
public User(String id, String username) {
this.id = id;
this.username = username;
}
public User() {
}
public String getHead_url() {
return head_url;
}
public void setHead_url(String head_url) {
this.head_url = head_url;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
2.6 activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录后获取用户信息"
/>
</LinearLayout>
RxBinding地址
https://github.com/JakeWharton/RxBinding
RxJava中文文档(PDF/ePub/Mobi格式下载 会全面一点)
https://mcxiaoke.gitbooks.io/rxdocs/content/
RxJava英文文档
http://reactivex.io/documentation/operators.html#alphabetical
参考视频:
https://www.cniao5.com/course/lessons/10120
源码下载:
https://download.csdn.net/download/zhaihaohao1/10748103
源码----RxJavaCN2----app4
扫描二维码关注公众号,回复:
3758558 查看本文章
![](/qrcode.jpg)