【发布时间】:2016-07-13 14:42:30
【问题描述】:
我有一个小时使用 RxJava 的经验,我正在尝试在我的项目中实现它,而不是使用接口和侦听器。
我有一个异步任务,它在单独的模块中调用谷歌云端点方法,并在完成后收到List<Profile>。
在异步任务的onPostExecute() 方法中,我调用 onNext 以便任何订阅者都收到此数据。
这是AsyncTask 的样子:
private BirthpayApi mApi;
private String mUserId;
private ReplaySubject<List<Profile>> notifier = ReplaySubject.create();
public GetFriends(String userId) {
mUserId = userId;
}
public Observable<List<Profile>> asObservable() {
return notifier;
}
@Override
protected List<Profile> doInBackground(Void... params) {
if (mApi == null) {
BirthpayApi.Builder builder = new BirthpayApi.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
// options for running against local devappserver
// - 10.0.2.2 is localhost's IP address in Android emulator
// - turn off compression when running against local devappserver
.setRootUrl("http://10.0.2.2:8080/_ah/api/")
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
@Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
});
mApi = builder.build();
}
try {
return mApi.getFriends(mUserId).execute().getItems();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(List<Profile> friends) {
super.onPostExecute(friends);
notifier.onNext(friends);
}
然后,在我的 Fragment 中,我想从调用 onNext() 方法的异步任务中收集这些数据。因此,我在声明也扩展了 Fragment 的类时使用implements Action1<List<Profile>>。
在来自 Action1 接口的onCall() 方法中,我收集从 Async 任务发送的数据:
@Override
public void call(List<Profile> profiles) {
if (profiles.size() > 0) {
updateAdapter(profiles);
} else
setUpNoFriendsViews();
}
我跟随树屋,但他们使用一个对象来建模他们的数据,这成为可观察的而不是使用异步类,并且他们使用适配器作为观察者。我做错了吗,无论如何我如何让它工作?
【问题讨论】:
-
所以您希望您的 observable 在您拨打电话时发出更新的个人资料列表?
-
然后你想要一个 observable 来观察会改变你的列表的事件,并通过在函数中调用你的 api 将它映射到你更新的列表中。要异步执行此操作,您必须使用 RXbindings 库或调用继续使用您的异步调用
-
你能给我一个例子吗(用代码)?
-
stackoverflow.com/questions/33415151/… 我提供的答案创建了一个可观察的事件并将其映射到一个字符串。然后我在订阅中打我的休息电话。我所做的有所不同,但它可能会给你一个更好的主意。
-
这段代码应该放在哪里,activity?
标签: java android android-asynctask rx-java