Android Rxjava2,2-在两个类之间传输数据。

Android Rxjava2,2-在两个类之间传输数据。,android,retrofit2,rx-java2,Android,Retrofit2,Rx Java2,我有简单的可观察的 public interface CitiesApi { @GET("location/cities") Observable<List<City>> getCities(@Query("country") String countryId); } 如您所见,我写了一条评论“我想在每个新请求中更新mList值”。 当演示者每次提出请求时,如何在Manager类中更新mList 好吧,我答应过你: 假设您设置了适配器并准备了mList,

我有简单的可观察的

public interface CitiesApi {
    @GET("location/cities")
    Observable<List<City>> getCities(@Query("country") String countryId);
}
如您所见,我写了一条评论“我想在每个新请求中更新mList值”。
当演示者每次提出请求时,如何在Manager类中更新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());
        }
    });
}
因为我有一个数据数组,所以我还定义了该数组中的数据类型(即在请求调用函数中将传递给
chatsdas
变量的确切数据)

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
)来实现所需的操作。以下是所有运算符的列表-

以下是您如何做到这一点:

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
                    }
                });
    }
}
类管理器{
List mList=new ArrayList();
公共可观测getCitiesObservable(字符串countryId){
返回CitiesApi.getCities(countryId)
.subscribeOn(Schedulers.io())
.map(新函数(){
@凌驾
公共列表应用(列表城市)引发异常{
//完成任务并返回列表对象
}
});
}
}

您是否序列化了您从该请求中得到的响应?您是什么意思?我会用我的方式发布一个答案,也许您会更好地理解。我正在等待。谢谢。谢谢,但我想用rxjava来做。我想让我的类管理器获取数据,哪个类演示者将接收到确定,但更新列表的想法是错误的应该是一样的,你得到新的数据,并将其重新设置为列表我理解。但是你用一个类来做,如果我有20个以上的管理器呢?你的代码看起来不是通用的,没有rx。我希望我的类管理器类似于下面的return CitiesApi.getCities(countryId).doOnNext(在类管理器中保存数据);Pfff..您想在类管理器中保存数据..这听起来不适合我。
@GET(RestClient.API_GET_CHATS)
Call<Chats> getChats(@Query("username") String username);
public class Chats implements Serializable {
private ArrayList<ChatsData> chats;

public ArrayList<ChatsData> getChats() {
    return chats;
}

public void setChats(ArrayList<ChatsData> chats) {
    this.chats = chats;
}
}
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
}
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
                    }
                });
    }
}