Retrofit2学习之九:与RxJava结合

需求:实现用户登录,然后通过登陆返回的用户Id获取用户信息。

普通实现

创建业务请求接口

1
2
3
4
5
6
public interface Api {

@POST("login/json")
Call<ResponseResult> login(@Body UserParam param);

}

创建一个Retrofit的实例,然后利用Retrofit实例创建接口对象和调用接口方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public void login(View view) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SERVER_ADDRESS)
.addConverterFactory(GsonConverterFactory.create())
.build();

api = retrofit.create(Api.class);

api.login(new UserParam("AppBlog.CN", "123456")).enqueue(new Callback<ResponseResult>() {
@Override
public void onResponse(Call<ResponseResult> call, Response<ResponseResult> response) {
if (response.isSuccessful()) {
int userId = response.body().getUserId();
api.getUserInfoWithPath(userId).enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if (response.isSuccessful()) {
User user = response.body();
Toast.makeText(MainActivity.this, user.getUsername(), Toast.LENGTH_SHORT).show();
}
}

@Override
public void onFailure(Call<User> call, Throwable t) {
Log.i(TAG, "请求失败: " + t.getLocalizedMessage());
}
});
}
}

@Override
public void onFailure(Call<ResponseResult> call, Throwable t) {
Log.i(TAG, "登录失败: " + t.getLocalizedMessage());
}
});
}

缺点:代码繁琐,嵌套多不方便扩展

与RxJava结合

添加依赖

1
2
3
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.2.3'

创建业务请求接口

1
2
3
4
5
6
public interface Api {

@POST("login/json")
Observable<ResponseResult> loginWithRx(@Body UserParam param);

}

创建一个Retrofit的实例,然后利用Retrofit实例创建接口对象和调用接口方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//嵌套写法
public void loginWithRx(View view) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SERVER_ADDRESS)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();

api = retrofit.create(Api.class);

api.loginWithRx(new UserParam("AppBlog.CN", "123456")).subscribeOn(Schedulers.io()).subscribe(new Subscriber<ResponseResult>() {
@Override
public void onCompleted() {

}

@Override
public void onError(Throwable e) {
e.printStackTrace();
Log.i(TAG, "登录失败: " + e.getLocalizedMessage());
}

@Override
public void onNext(ResponseResult responseResult) {
api.getUserInfoWithRx(responseResult.getUserId())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<User>() {
@Override
public void call(User user) {
Toast.makeText(MainActivity.this, user.getUsername(), Toast.LENGTH_SHORT).show();
}
});
}
});
}

优点:

  • 使用RxJava适配器 addCallAdapterFactory(RxJavaCallAdapterFactory.create()) 可以实现返回Body自动转换为ResponseResult对象
  • 使用 subscribeOn(Schedulers.io())observeOn(AndroidSchedulers.mainThread()) 轻松实现IO线程和UI线程间的切换

缺点:代码嵌套仍然过多,不方便扩展

RxJava链式写法

创建业务请求接口

1
2
3
4
5
6
public interface Api {

@GET("user/{id}")
Observable<User> getUserInfoWithRx(@Path("id") int userId);

}

创建一个Retrofit的实例,然后利用Retrofit实例创建接口对象和调用接口方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//链式写法
public void loginWithRxChain(View view) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SERVER_ADDRESS)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();

api = retrofit.create(Api.class);

api.loginWithRx(new UserParam("AppBlog.CN", "123456")).flatMap(new Func1<ResponseResult, Observable<User>>() {
@Override
public Observable<User> call(ResponseResult responseResult) {
return api.getUserInfoWithRx(responseResult.getUserId());
}
}).doOnNext(new Action1<User>() {
@Override
public void call(User user) {
//download avatar
}
}).doOnSubscribe(new Action0() {
@Override
public void call() {

}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<User>() {
@Override
public void call(User user) {
Toast.makeText(MainActivity.this, user.getUsername(), Toast.LENGTH_SHORT).show();
}
});
}

优点:代码链式写法,结构清晰,扩展方便

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :