我正在尝试向Pusher api发出post请求,但在返回正确的类型时遇到问题,因为类型不匹配;找到: scala.concurrent.Futureplay.api.libs.ws.Response必需: play.api.libs.ws.Response
def trigger(channel:String, event:String, message:String): ws.Response = {
val domain = "api.pusherapp.com"
val url = "/apps/"+appId+"/channels/"+channel+"/events";
val body = message
val params = List(
("auth_key", key),
("auth_timestamp", (new Date().getTime()/1000) toInt ),
("auth_version", "1.0"),
("name", event),
("body_md5", md5(body))
).sortWith((a,b) => a._1 < b._1 ).map( o => o._1+"="+URLEncoder.encode(o._2.toString)).mkString("&");
val signature = sha256(List("POST", url, params).mkString("\n"), secret.get);
val signatureEncoded = URLEncoder.encode(signature, "UTF-8");
implicit val timeout = Timeout(5 seconds)
WS.url("http://"+domain+url+"?"+params+"&auth_signature="+signatureEncoded).post(body
}发布于 2013-03-29 05:51:04
您使用post发出的请求是异步的。该调用立即返回,但不返回Response对象。相反,它返回一个Future[Response]对象,一旦异步完成http请求,该对象将包含Response对象。
如果您希望在请求完成之前阻止执行,请执行以下操作:
val f = Ws.url(...).post(...)
Await.result(f)请参阅有关futures here的更多信息。
发布于 2013-03-29 05:50:52
只需附加一个map
WS.url("http://"+domain+url+"?"+params+"&auth_signature="+signatureEncoded).post(body).map(_)发布于 2013-03-29 06:11:04
假设你不想创建一个阻塞应用程序,你的方法也应该返回一个Future[ws.Response]。让你的未来上升到控制器,在那里你使用Async { ... }返回一个AsyncResult,然后让Play来处理剩下的事情。
控制器
def webServiceResult = Action { implicit request =>
Async {
// ... your logic
trigger(channel, event, message).map { response =>
// Do something with the response, e.g. convert to Json
}
}
}https://stackoverflow.com/questions/15692681
复制相似问题