Rxjava2、Retrofit2 - 在两个 类 之间传输数据。
Rxjava2, Retrofit2 - transfer data between two classes.
我有简单的 Observable。
public interface CitiesApi {
@GET("location/cities")
Observable<List<City>> getCities(@Query("country") String countryId);
}
我还有两个 classes:
class Manager {
List<City> mList = new ArrayList();
public Observable<List<City>> getCitiesObservable(String countryId) {
// I want to update mList value in each new request;
return CitiesApi.getCities(countryId);
}
第二个class:
class Presenter {
public void request() {
Manager.getCitiesObservable("us")
.subscribeOn(Schedulers.newThread)
.observeOn(AndroidSchedulers.mainThread)
.subscribe(new ......)
}
如你所见,我写了一条评论"I want to update mList value in each new request"。
每次 Presenter 发出请求时,如何在 Manager class 中更新 mList?
好的,这是我承诺的:
考虑到你设置了适配器并准备好你的mList
,我就是这样做的。
调用请求并将响应主体放在变量中的函数
private void makeGetMyChatsRequest(String searchWord){
RestClient.getApi().getChats(searchWord).enqueue(new Callback<Chats>() {
@Override
public void onResponse(Call<Chats> call, Response<Chats> response) {
if (response.isSuccessful())
if (response.code() == 200){
chatsDatas = response.body().getChats();
chatAdapter.notifiDataSetChanged();
}
}
@Override
public void onFailure(Call<Chats> call, Throwable t) {
Log.d(TAG, "-=onFailure=-\n" + t.getMessage());
}
});
}
这里是改装界面
@GET(RestClient.API_GET_CHATS)
Call<Chats> getChats(@Query("username") String username);
它响应的对象 type
,在我的例子中 <Chats>
public class Chats implements Serializable {
private ArrayList<ChatsData> chats;
public ArrayList<ChatsData> getChats() {
return chats;
}
public void setChats(ArrayList<ChatsData> chats) {
this.chats = chats;
}
}
并且因为我有一个数据数组,所以我还在这个数组中定义了数据类型(将在请求调用函数上传递给 chatsDatas
变量的确切数据。
public class ChatsData implements Serializable {
private int chat_room_id;
private int read;
private int user_id;
private String name;
private String picture;
private int is_online;
private LastMessageData last_message;
//here will be getters & setters for each field
}
你应该使用 RxJava 的转换运算符(如 map
)来实现你想做的事情。这是所有运算符的列表 - ReactiveX - Operators
以下是您的操作方法:
class Manager {
List<City> mList = new ArrayList();
public Observable<List<City>> getCitiesObservable(String countryId) {
return CitiesApi.getCities(countryId)
.subscribeOn(Schedulers.io())
.map(new Function<List<City>, List<City>>() {
@Override
public List<City> apply(List<City> cities) throws Exception {
// Do your stuff and return a List<City> object
}
});
}
}
我有简单的 Observable。
public interface CitiesApi {
@GET("location/cities")
Observable<List<City>> getCities(@Query("country") String countryId);
}
我还有两个 classes:
class Manager {
List<City> mList = new ArrayList();
public Observable<List<City>> getCitiesObservable(String countryId) {
// I want to update mList value in each new request;
return CitiesApi.getCities(countryId);
}
第二个class:
class Presenter {
public void request() {
Manager.getCitiesObservable("us")
.subscribeOn(Schedulers.newThread)
.observeOn(AndroidSchedulers.mainThread)
.subscribe(new ......)
}
如你所见,我写了一条评论"I want to update mList value in each new request"。 每次 Presenter 发出请求时,如何在 Manager class 中更新 mList?
好的,这是我承诺的:
考虑到你设置了适配器并准备好你的mList
,我就是这样做的。
调用请求并将响应主体放在变量中的函数
private void makeGetMyChatsRequest(String searchWord){
RestClient.getApi().getChats(searchWord).enqueue(new Callback<Chats>() {
@Override
public void onResponse(Call<Chats> call, Response<Chats> response) {
if (response.isSuccessful())
if (response.code() == 200){
chatsDatas = response.body().getChats();
chatAdapter.notifiDataSetChanged();
}
}
@Override
public void onFailure(Call<Chats> call, Throwable t) {
Log.d(TAG, "-=onFailure=-\n" + t.getMessage());
}
});
}
这里是改装界面
@GET(RestClient.API_GET_CHATS)
Call<Chats> getChats(@Query("username") String username);
它响应的对象 type
,在我的例子中 <Chats>
public class Chats implements Serializable {
private ArrayList<ChatsData> chats;
public ArrayList<ChatsData> getChats() {
return chats;
}
public void setChats(ArrayList<ChatsData> chats) {
this.chats = chats;
}
}
并且因为我有一个数据数组,所以我还在这个数组中定义了数据类型(将在请求调用函数上传递给 chatsDatas
变量的确切数据。
public class ChatsData implements Serializable {
private int chat_room_id;
private int read;
private int user_id;
private String name;
private String picture;
private int is_online;
private LastMessageData last_message;
//here will be getters & setters for each field
}
你应该使用 RxJava 的转换运算符(如 map
)来实现你想做的事情。这是所有运算符的列表 - ReactiveX - Operators
以下是您的操作方法:
class Manager {
List<City> mList = new ArrayList();
public Observable<List<City>> getCitiesObservable(String countryId) {
return CitiesApi.getCities(countryId)
.subscribeOn(Schedulers.io())
.map(new Function<List<City>, List<City>>() {
@Override
public List<City> apply(List<City> cities) throws Exception {
// Do your stuff and return a List<City> object
}
});
}
}