RxJava和Retrofit - 连锁服务响应

时间:2016-06-26 20:01:03

标签: java android rx-java retrofit2

RxJava新手。试图实现一些微不足道的事情,但似乎无法绕过整个POJO范例到目前为止。我想要的是一个接一个地进行两个服务调用,并简单地返回由两个调用的响应组成的最终自定义 RestWebClient.get().getFirstImage() .flatMap(firstImage -> RestWebClient.get().getSecondImage()) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(firstImage -> mainFragmentPresenterInterface.showImages(firstImage)); 。这里要注意的是,这两个呼叫是相互独立的。只有当第一个呼叫成功返回时,第二个呼叫才会触发。第二个不使用第一次服务呼叫中的任何数据。到目前为止我有什么。

content
  1. 我不知道如何将两个单独的网络调用的响应链接起来并将它们作为单个POJO返回给演示者。

  2. 另外,如果getFirstImage()成功,我们怎样才能确保调用getSecondImage()?

  3. 感谢。

1 个答案:

答案 0 :(得分:1)

你开始正确,我只是稍微修改你的代码片段:

const statementOptions = [
  '1:accepted:1',
  '3:accepted:3',
  '4:accepted:4',
  ...
]

nightmare.goto('https://www.homesavings.com')
//login
.viewport(900, 800)
.wait('form[name="Login"]')
.insert('#userid', config.userid)
.insert('#password', config.password)
.click('#submit input')

// go to Online Statements page
.wait('a[data-app-code="Online Statements"]')
.goto('https://www.hslonline2.com/onlineserv/HB/OnlineStatements.cgi')
.evaluate(statementOptions => statementOptions, statementOptions)
.then(statementOptions => {
  return statementOptions.reduce(function(acc, option, index) {
    console.log(`reduce it: option=${option} index=${index}`)
    return acc.then(function(results) {
      return nightmare
        // wait for the account select box to load..
        .wait('select[name="acctRef"]')
        .select('select[name="acctRef"]', option)
        //.wait(1000)
        .click('input[type="submit"]')
        //.wait(2000)
        .wait('td a img')
        .click('td a')
        .download(`${downloadDir}/bankPDF_${index}`)
        .then(info => {
          console.log(`after download. info -> ${JSON.stringify(info, null, 2)}`)
          results.push(info)
          return results
        })
      })
   }, Promise.resolve([])).then(infos => infos)
})