Java 使用改进2和RxAndroid从Spring WebFlux获得响应
我不确定如何设置android端来反序列化这个 这是我在服务器上的定义:Java 使用改进2和RxAndroid从Spring WebFlux获得响应,java,kotlin,retrofit2,rx-android,spring-webflux,Java,Kotlin,Retrofit2,Rx Android,Spring Webflux,我不确定如何设置android端来反序列化这个 这是我在服务器上的定义: @GetMapping("/quotes-reactive-paged") fun getQuoteFlux(@RequestParam(name = "page") page: Int, @RequestParam(name = "size") size: Int): Flux<Quote> { return quoteMongoReactiv
@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()))
}
这是我在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
在服务器端,这是一个模型:
data class Quote(val id: String, val book: String, val content: String)
它似乎没有将json转换为对象,但我不明白为什么
更新
所以我用@GET
添加了@Streaming
,并尝试了这个方法,但Okhttp3.responsebook无法工作,因为它无法编译
.subscribe(new DisposableObserver<ResponseBody>() {
@Override
public void onNext(ResponseBody responseBody) {
.subscribe(新的DisposableObserver(){
@凌驾
public void onNext(ResponseBody ResponseBody){
我没有使用flux,但您发送的JSON格式不正确,它需要一个Quote
对象,因此它应该以{
开头
您正在发送一个列表,因此应该将界面更改为
@GET("quotes-reactive-paged")
Observable<List<Quote>> queryReactivePaging(@Query("page") int page, @Query("size") int size);
@GET(“引用被动页面”)
可观察的queryReactivePaging(@Query(“page”)int page,@Query(“size”)int size);
现在我不记得Rx是否能与列表
一起工作
无论如何,它将不起作用,因为列表以
[
开头,所以请更改您的服务,以正确设置响应的格式,并使您的接收接口适应您希望接收的任何内容。示例工单或命令起作用
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
您得到的是
内容类型:text/event stream
,它似乎在实际的JSON之前添加了数据:
。可能是SSE
哪种改型不支持…@EpicPandaForce-我不知道数据是否是实际返回的内容的一部分,所以我将研究事件流是否是我的问题。谢谢,我认为是这样是!考虑到SSE有一个名为data:
的部分,我实际上非常确定。如果你能删掉它,它会起作用的。明天我将添加一个拦截器@EpicPandaForce,看看我是否能去掉响应中的“data:”。我以前没有尝试过。你返回的JSON似乎无效。请在任何在线JSON lint工具上检查。
data class Quote(val id: String, val book: String, val content: String)
.subscribe(new DisposableObserver<ResponseBody>() {
@Override
public void onNext(ResponseBody responseBody) {
@GET("quotes-reactive-paged")
Observable<List<Quote>> queryReactivePaging(@Query("page") int page, @Query("size") int size);
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