代码之家  ›  专栏  ›  技术社区  ›  James Black

使用Reform2和RxAndroid从Spring WebFlux获取响应

  •  5
  • James Black  · 技术社区  · 6 年前

    我不知道如何设置android端来反序列化它。

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

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

    在android方面,我有以下回应:

    @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.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
    

    这是android端的模型:

    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转换为对象,但我不明白为什么。

    使现代化

    所以我补充道 @Streaming 使用 @GET 尝试了这个,但Okhttp3没有办法。Responsebody工作,因为它不会编译。

               .subscribe(new DisposableObserver<ResponseBody>() {
                    @Override
                    public void onNext(ResponseBody responseBody) {
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Gonzalo    6 年前

    我没有使用flux,但您正在发送格式错误的JSON,它需要 Quote 对象,因此它应该以 {

    您正在发送一个列表,因此应该将界面更改为

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

    现在我不记得Rx是否能与 List<>

    无论如何,它不会工作,因为列表以 [ ,因此更改您的服务以正确设置响应格式,并根据您期望接收的内容调整您的Rx接口。

        2
  •  0
  •   uminder    4 年前

    示例工单或命令有效。

    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