0
我遇到了一個我目前無法解決的問題。我正在做多個http請求,在每個響應中,它應該有一個Array[DTAnnotation]
。我想積累所有結果列表到一個(這不是問題在這裏)。我的問題是我無法返回WSResponse
的結果。我嘗試什麼:從多個HTTP請求中提取並累加結果
import mymodel.{DTFeatures, DTResponse, DTRequest, DTAnnotations}
def checkForSpike
(
awsKey : String,
collection : JSONCollection,
record : Record,
features : Array[DTFeatures]
) : Unit = {
val url = config.getString("url").getOrElse
val holder = WS.url(url)
val complexHolder =
holder.withHeaders(("Content-Type","application/json"))
// excepting result is List[Array[DTAnnotations]]
val test : List[Array[DTAnnotations]] =
for(feature <- features) yield {
val dtFeature = Json.stringify(Json.toJson(DTRequest(feature)))
val futureResponse = complexHolder.post(dtFeature)
Logger.info("Make the HTTP POST Request in " + (t1 - t0) + " msecs")
futureResponse.map { response =>
Logger.info("Get response in " + (System.currentTimeMillis - t1))
if(response.status == 200) {
response.json.validate[DTResponse].map { dtResponse =>
// I want to return this
dtResponse.annotations
}.recoverTotal { _ =>
Logger.debug("invalid json")
}
} else {
Logger.debug(Json.prettyPrint(Json.obj("status" -> response.status, "body" -> response.body)))
}
}
Await.result(futureResponse, 10.seconds)
}
}
因爲響應是一個Future
,我嘗試添加Await
得到註解,但我在打字階段一個錯誤:
[error] found : Array[play.api.libs.ws.WSResponse]
[error] required: List[Array[DTAnnotation]]
我怎麼能解決這個問題?謝謝 !
工程像魅力!也感謝您的時間和意見 – alifirat
不客氣。一旦你有它的工作,你可以改進一點。避免等待並閱讀有關組合期貨的信息。 Daniel Westheide的教程非常好。這是關於期貨的章節http://danielwestheide.com/blog/2013/01/09/the-neophytes-guide-to-scala-part-8-welcome-to-the-future.html –
您是否打算使用'flatMap'而不是'map'? – alifirat