我正在研究使用 Retrofit2 和 RxJava 的 Java 8 REST 客户端设置。使用时GsonConverterFactory一切都会按预期进行。当切换到 a 时JacksonConverterFactory,我根本看不到任何结果(但也不例外)。根据日志,REST 调用本身是正常的。
GitHub上的完整示例。
在 RX JAVA(java8) 中,如何保留 previousflatMap或map.
public void createAccount(]) {
JsonObject payload = routingContext.getBodyAsJson();
socialService.getOAuthToken(payload)
.flatMap(token -> {
return getAllAccounts(token);
})
.flatMap(accounts -> {
// Save accounts with TOKENS
})
.subscribe(accountID -> {
response(accountID);
);
}
Run Code Online (Sandbox Code Playgroud)
所以在上面的代码中,第二步flatMap我怎样才能token从之前的flatMap.
units我想在和 都variables设置时执行某些操作(通过Single<T>, NOT Observable)。怎么做?
// getUserId(), getSomething(), getSomethingElse() all return Single<T>
getUserId().flatMap { getSomething(it) }.subscribe({ data -> units = data })
getUserId().flatMap { getSomethingElse(it) }.subscribe({ data -> variables = data })
execute(units, variables)
Run Code Online (Sandbox Code Playgroud) 我想通过 Vertx 中的 EventBus 同步发送多条消息。我想发送一条消息,等待它,然后再发送下一条消息。地址是一样的。如果是默认的话我该怎么做?还是必须使用executeBlocking代码?
这是我的代码。
public class EventBusSync {
private Vertx vertx = Vertx.vertx();
private static final String SERVICE_ADDRESS = "service.worker";
public void sentViaEvBus() {
String message1 = "message1";
String message2 = "message2";
String reply1 = sendCommand(SERVICE_ADDRESS,message1);
String reply2 = sendCommand(SERVICE_ADDRESS,message2);
}
private String sendCommand(String address, String command) {
String message;
vertx.eventBus().send(address,command, handler -> {
if(handler.succeeded()) {
log.info("success");
} else {
log.error("error",handler.cause());
throw new RuntimeException("ERROR");
}
message = handler.result.body();
});
return message;
}
}
Run Code Online (Sandbox Code Playgroud)
所以在这里,如果发送第一个命令并且发生了一些事情,我想中断下一个事件总线的发送。
谢谢
private ModelObject model;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
}
private void readFile() {
if (model == null) {
Gson gson = new Gson();
final String helpItem = “my _file.json”;
InputStream stream = null;
try {
stream = getResources().getAssets().open(helpItem);
Reader reader = new InputStreamReader(stream);
model = gson.fromJson(reader, ModelObjects.class);
reader.close();
stream.close();
} catch (IOException e) {
Timber.w(e);
} finally {
fileclose();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我使用此代码的代码我正在从资产文件夹读取文件并解析为模型对象但我想在后台线程而不是主线程中读取此文件因此并获取数据主线程请建议我如何在后台实现读取文件线程并在主线程中获取值。
我使用 LiveData 显示 Room 中表中的记录数。我调用一个函数来检索该计数,并在收到该计数后调用观察者来显示该计数。这按预期工作。但我还运行一个服务,该服务从后端检索数据并将其存储在我从中读取计数的同一个表中。但是每当存储数据时,每次都会调用可观察对象并更新显示的计数。我不知道为什么会发生这种情况。事实上我确实希望这一切发生。我只是不明白为什么会发生这种情况。当我运行代码来检索计数时,它是使用 RxJava 完成的。因此,当调用完成时,我看不出为什么计数的可观察值会随着每个数据存储而更新。唯一可能的原因是 Room 会跟踪我的计数查询并在存储数据时执行它。那可能吗?这是我获取计数的代码:
在我的片段中观察到:
viewModel.onConnectionsCountRetrieved.observe(this, Observer { count ->
var title = getString(R.string.connections)
if (count > 0)
title += " (" + "%,d".format(count) + ")"
(activity as MainActivity).getSupportActionBar()?.title = title
})
Run Code Online (Sandbox Code Playgroud)
在我的视图模型中:
val onConnectionsCountRetrieved: MutableLiveData<Int> = MutableLiveData()
@SuppressLint("CheckResult")
fun getConnectionsCount() {
val disposable = connectionsBO.getConnectionsCount()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ count -> onConnectionsCountRetrieved.postValue(count) },
{ ex -> App.context.displayErrorMessage(R.string.problem_retrieving_total_connection_count) }
)
disposables.add(disposable)
}
Run Code Online (Sandbox Code Playgroud) 不确定如何处理插入方法的返回类型。
@Dao
interface ProductDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAlll( product:List<Product>):List<Product>
}
Run Code Online (Sandbox Code Playgroud)
override fun getFactoriProduct(): Observable<List<Product>> {
return Observable.create { emitter ->
api.getProductRemote()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
if (it != null) {
emitter.onNext(db.productDao().insertAlll(it))
Timber.e("request->$it")
}
}, {
emitter.onNext(db.productDao().getProduct())
it.printStackTrace()
Timber.e("ErrorRequest->$it")
})
}
}
Run Code Online (Sandbox Code Playgroud)
活动.kt
fun init() {
mainViewmodel.getProduct().subscribe {
val adapter = ProductAdapter(it)
RecyclerView2.layoutManager = LinearLayoutManager(this, LinearLayout.HORIZONTAL, false)
RecyclerView2.adapter = adapter
adapter.update(it)
}.addTo(this.CompositeDisposable)
Run Code Online (Sandbox Code Playgroud)
如何处理插入方法的返回类型。公共抽象 java.util.List insertAll(@org.jetbrains.annotations.NotNull()
我有一个数据类报告
data class Report(var id: Int? = null, var user_name: String? = null)
Run Code Online (Sandbox Code Playgroud)
我有一份报告清单。例如 :
val reports = listOf(
Report(1, "Mike"),
Report(2, "John"),
Report(3, "Ann"),
Report(4, "Mike"),
Report(5, "Bob"),
Report(6, "Carl"),
Report(7, "Donald"),
Report(8, "John"),
Report(9, "Ann"),
Report(10, "Bob"))
Run Code Online (Sandbox Code Playgroud)
如何将此报告列表转换为List<List<Reports>>将使用 RxJava 由 user_name 探查的报告?id 需要的最终变体是这样的:
val reports_grouped_by_user_name = listOf(
listOf(Report(1, "Mike"),Report(4, "Mike")),
listOf(Report(2, "John"),Report(8, "John")),
listOf(Report(3, "Ann"),Report(9, "Ann")),
listOf(Report(5, "Bob"),Report(10, "Bob")),
listOf(Report(6, "Carl")),
listOf(Report(7, "Donald")),
listOf(Report(10, "Bob")))
Run Code Online (Sandbox Code Playgroud) I am trying to follow a few code examples to pull a single record from the database to display, because it seems somewhat complicated to do so with LiveData. However, I am getting an error in the IDE.
无法解析符号“AndroidSchedulers”
在线上:
AndroidSchedulers.mainThread()
Run Code Online (Sandbox Code Playgroud)
在这次通话中:
viewModel.getScenario(scenarioId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new DisposableSingleObserver<Scenario>() {
@Override
public void onSuccess(Scenario scenario) {
}
});
Run Code Online (Sandbox Code Playgroud)
我的年级是这样的:
implementation 'androidx.room:room-rxjava2:2.1.0'
Run Code Online (Sandbox Code Playgroud) 我目前正在学习RxJava/RxAndroid的基础知识,并尝试创建一个非常基本的Retrofit测试应用程序.
我在这个例子中使用RxJava 1.2.6.
但是,我一直没能找到如何在这是一个列表的对象执行功能的任何明显的例子中可观察的对象.
例如,如果我有以下POJO
public class AgencyResponse {
private List<Agency> agencies;
private int total;
private int count;
private int offset;
public List<Agency> getAgencies() {
return agencies;
}
public int getTotal() {
return total;
}
public int getCount() {
return count;
}
public int getOffset() {
return offset;
}
}
Run Code Online (Sandbox Code Playgroud)
以及
public class Agency {
private int id;
private String name;
private String abbrev;
public int getId() {
return id;
}
public String getName() {
return name;
} …Run Code Online (Sandbox Code Playgroud)