有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用改型2和RxAndroid从Spring WebFlux获得响应

我不确定如何设置安卓端来反序列化它

这是我在服务器上的定义:

@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()))
    }

在安卓方面,我有这样的回答:

@GET("quotes-reactive-paged")
Observable<Quote> queryReactivePaging(@Query("page") int page,
                                              @Query("size") int size);

以下是我用来处理的:

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");
                }
            })

这是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?"}

这是我在logcat中得到的,但我确实删掉了一些数据元素

05-04 21:41:58.056 13277-13338/reactive.安卓.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.安卓.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.安卓.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.安卓.cigna.com.reactiveexample I/zygote: Do partial code cache collection, code=48KB, data=47KB

这是安卓端的模型:

public class Quote {
    public String id;
    public String content;
    public String book;
}

在服务器端,这是一个模型:

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) {

共 (1) 个答案

  1. # 1 楼答案

    示例工作指令或命令有效

    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