[英]Using retrofit2 and RxAndroid to get response from Spring WebFlux
I am not certain how to set up the android side to deserialize this.我不确定如何设置 android 端来反序列化。
This is my definition on the server:这是我在服务器上的定义:
@GetMapping("/quotes-reactive-paged")
fun getQuoteFlux(@RequestParam(name = "page") page: Int,
@RequestParam(name = "size") size: Int): Flux<Quote> {
return quoteMongoReactiveRepository.retrieveAllQuotesPaged(PageRequest.of(page, size))
.delayElements(Duration.ofMillis(DELAY_PER_ITEM_MS.toLong()))
}
On the android side I have this to get the response:在 android 方面,我有这个来得到回应:
@GET("quotes-reactive-paged")
Observable<Quote> queryReactivePaging(@Query("page") int page,
@Query("size") int size);
And here is what I use to process:这是我用来处理的:
mReactiveQuoteService.queryReactivePaging(page, size)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(cityResponse -> {
return cityResponse;
})
.subscribeWith(new DisposableObserver<Quote>() {
@Override
public void onNext(Quote quote) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
System.out.println(quote.content);
reactiveList.setText(reactiveList.getText() + "\n" + quote.content);
} else {
System.out.println(quote.content);
reactiveList.setText(reactiveList.getText() + "\n" + quote.content);
}
}
@Override
public void onError(Throwable e) {
System.out.println(e);
}
@Override
public void onComplete() {
System.out.println("done");
}
})
This is an example of a response, printed out by OkHttp3:这是一个响应示例,由 OkHttp3 打印:
data:{"id":"1025","book":"El Quijote","content":"-En efecto -dijo Sancho-, ¿qué es lo que vuestra merced quiere hacer en este tan remoto lugar?"}
And here is what I get in logcat, but I did cut out some of the data elements.这是我在 logcat 中得到的,但我确实删除了一些数据元素。
05-04 21:41:58.056 13277-13338/reactive.android.cigna.com.reactiveexample D/OkHttp: <-- 200 OK http://192.168.1.104:9094/quotes-reactive-paged?page=3&size=20&username=demo (149ms)
transfer-encoding: chunked
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS
Access-Control-Allow-Headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range
Access-Control-Expose-Headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range
Content-Type: text/event-stream
05-04 21:42:00.040 13277-13338/reactive.android.cigna.com.reactiveexample D/OkHttp: data:{"id":"1052","book":"El Quijote","content":"-¡Ta, ta! -dijo Sancho-. ¿Que la hija de Lorenzo Corchuelo es la señora Dulcinea del Toboso, llamada por otro nombre Aldonza Lorenzo?"}
data:{"id":"1053","book":"El Quijote","content":"-Ésa es -dijo don Quijote-, y es la que merece ser señora de todo el universo."}
data:{"id":"1068","book":"El Quijote","content":"Y, habiéndola escrito,se la leyó; que decía ansí:"}
<-- END HTTP (10363-byte body)
05-04 21:42:00.047 13277-13277/reactive.android.cigna.com.reactiveexample I/System.out: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
05-04 21:42:09.672 13277-13282/reactive.android.cigna.com.reactiveexample I/zygote: Do partial code cache collection, code=48KB, data=47KB
This is the model on the android side:这是android端的模型:
public class Quote {
public String id;
public String content;
public String book;
}
On the server side this is the model:在服务器端,这是模型:
data class Quote(val id: String, val book: String, val content: String)
It appears that it isn't turning the json into an object, but I don't see why.似乎它没有将 json 变成一个对象,但我不明白为什么。
UPDATE更新
So I added @Streaming
with the @GET
and tried this, but there is no way for Okhttp3.Responsebody to work, as it won't compile.因此,我增加
@Streaming
与@GET
和尝试这样做,但没有办法Okhttp3.Responsebody工作,因为它不会编译。
.subscribe(new DisposableObserver<ResponseBody>() {
@Override
public void onNext(ResponseBody responseBody) {
I have not worked with flux but you are sending a malformed JSON, it expects a Quote
object so it should start with {
.我没有使用过 Flux,但您发送的是格式错误的 JSON,它需要一个
Quote
对象,因此它应该以{
开头。
You are sending a list so you should change your interface to something like您正在发送一个列表,因此您应该将界面更改为
@GET("quotes-reactive-paged")
Observable<List<Quote>> queryReactivePaging(@Query("page") int page, @Query("size") int size);
Now I can't remember if Rx will work with List<>
.现在我不记得 Rx 是否可以与
List<>
。
Anyway it will not work because lists start with [
, so change you serve to correctly format the response and adapt your Rx interface to whatever you expect to receive.无论如何它不会起作用,因为列表以
[
开头,因此更改您可以正确格式化响应并将您的 Rx 接口调整为您期望接收的任何内容。
Example work order or commands work.示例工作订单或命令工作。
Observable.just("one", "two", "three", "four", "five")
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext {
valor - >
run {
//actios with valor
}
}
.subscribe(); //this necesary for processing for each element in doOnNext otherwise, it does nothing
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.