如何创建Observable并在以后的RxJava中使用Retrofit更新它们?

时间:2019-02-28 07:38:11

标签: android observable retrofit rx-java rx-java2

基本上,我需要创建一个没有初始值的Observable列表。我订阅了该可观察对象列表,并将根据所有可观察对象的响应提供所需的结果。我使用zip运算符。

问题是我需要首先创建Observable,将其添加到列表中并使用zip运算符。直到以后我通过Retrofit发出网络请求,并且我需要更新列表中observable的值,以便我的整个zip运算符都能正常工作。

但是,我没有找到一种方法来强制使用Retrofit的响应更新列表中的可观察对象。看起来很简单,但我没有找到任何解决方案。.唯一的想法是使用大量主题,并将其添加到列表中...

08:01| WARN | HttpMethodBase.java 1656 | Cookie rejected: "$Version=0; __cfduid=d05edb152ec46afbcb3763a2e231b959b1551337302; $Path=/; $Domain=.restcountries.eu". Illegal domain attribute ".restcountries.eu". Domain of origin: "restcountries.eu"
08:01| ERROR | CamelLogger.java 204 | Failed delivery for (MessageId: ID-DZS10W321-1551337302436-0-2 on ExchangeId: ID-DZS10W321-1551337302436-0-1). Exhausted after delivery attempt: 1 caught: org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking http://restcountries.eu/rest/v2/alpha/$%7Bbody%7D with statusCode: 400

Message History
---------------------------------------------------------------------------------------------------------------------------------------
RouteId              ProcessorId          Processor                                                                        Elapsed (ms)
[route1            ] [route1            ] [direct://restCall                                                             ] [       317]
[route1            ] [to1               ] [log:DEBUG?showBody=true&showHeaders=true                                      ] [         3]
[route1            ] [setHeader1        ] [setHeader[CamelHttpMethod]                                                    ] [         0]
[route1            ] [setHeader2        ] [setHeader[CamelHttpBaseUri]                                                   ] [         6]
[route1            ] [to2               ] [http://restcountries.eu/rest/v2/alpha/${body}                                 ] [       306]

Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------
org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking http://restcountries.eu/rest/v2/alpha/$%7Bbody%7D with statusCode: 400
    at org.apache.camel.component.http.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:243)
    at org.apache.camel.component.http.HttpProducer.process(HttpProducer.java:165)

1 个答案:

答案 0 :(得分:0)

如果我理解正确,那么您想实现 sub请求模型。对于此任务,您可以将运算符的链拆分为不同的执行流,然后将其合并起来,例如,与zip运算符合并。使用这种方法,您可以使用单一触发器创建完全独立的数据流。

Subject<Event> eventSubject = PublishSubject.create();

Observable<TriggerObject> mainRequest = eventSubject.flatMap((event) -> 
        RetrofitFactory.create().startProcess());
Observable<FirstSubResult> firstSubRequest = mainRequest.flatMap(tigger -> {
    // make first sub request
});
Observable<SecondSubResult> secondSubRequest = mainRequest.flatMap(tigger -> {
    // make second sub request
});
Observable<CompleteResult> resultObservable = Observable.zip(firstSubRequest, secondSubRequest,
        // zip function
        (first, second) -> {
            // combine result of sub requests to complete result object  
        });

现在您可以按事件开始请求流程:

// post your event. On button clicked for evxample
eventSubject.doOnNext(yourEvent);

注意:此答案显示了链接数据流序列的主要思想。这适用于其他类型的请求,您可以使用这种方法而无需进行修改