从异步请求播放框架scala

时间:2015-06-11 15:05:56

标签: multithreading scala playframework-2.0

我想从网络请求返回一个布尔值,并根据该布尔响应执行操作。现在我无法做到这一点因为Any类型没有转换为boolean。有人可以帮忙吗?

  def tetetetestae() = Action.async { implicit request =>
    request.session.get("email") match {
      case Some(email) =>
        for {
          t <- test("","","","","")
        } yield {
          if(t){
            Ok("")
          } else {
            Ok("")
          }
        }
    }
  }
}

  def test(email: String, firstName: String, lastName: String, c_password: String, n_password: String) = {
    WS.url("http://hire.monster.com:81/authenticateUser")
      .post(Map("email" -> Seq(email), "password" -> Seq(c_password)))
      .map(response =>
      if(response.body.contains("true")){
        for(result <- WS.url("http://localhost:9001/settings/updateUser")
          .post(Map("email" -> Seq(email), "firstName" -> Seq(firstName), "lastName" -> Seq(lastName), "n_password" -> Seq(n_password))))
          yield result.body.contains("true")
      } else {
        false
      })
  }

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

def test(email: String, firstName: String, lastName: String, c_password: String, n_password: String): Future[Boolean] = {
  WS.url("http://hire.monster.com:81/authenticateUser")
    .post(Map("email" -> Seq(email), "password" -> Seq(c_password)))
    .flatMap(response =>
    if(response.body.contains("true")){
      for(result <- WS.url("http://localhost:9001/settings/updateUser")
        .post(Map("email" -> Seq(email), "firstName" -> Seq(firstName), "lastName" -> Seq(lastName), "n_password" -> Seq(n_password))))
        yield result.body.contains("true")
    } else {
      Future(false)
    })
}

这将返回您需要的Future[Boolean]