类型不匹配; 发现:scala.concurrent.Future [play.api.libs.ws.Response]必需:play.api.libs.ws.Response

flu*_*bba 5 scala future playframework playframework-2.1

我正在尝试向Pusher api发布帖子请求,但是我无法返回正确的类型,我的类型不匹配; 发现:scala.concurrent.Future [play.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
}
Run Code Online (Sandbox Code Playgroud)

axe*_*l22 4

您发出的请求post是异步的。该调用立即返回,但不返回Response对象。相反,它返回一个对象,一旦 http 请求异步完成,Future[Response]该对象将包含该对象。Response

如果您想阻止执行直到请求完成,请执行以下操作:

val f = Ws.url(...).post(...)
Await.result(f)
Run Code Online (Sandbox Code Playgroud)

在这里查看更多关于期货的信息。