Android中RxJava使用4----請求兩次,第二次請求的引數是第一次請求的結果
阿新 • • 發佈:2018-12-17
思路: 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>
原始碼下載: