如何发送HTTP请求在斯卡拉发送HTTP请求
https://www.googleapis.com/language/translate/v2?
in scala and parse this JSON formatted response。
在此先感谢,
如何发送HTTP请求在斯卡拉发送HTTP请求
https://www.googleapis.com/language/translate/v2?
in scala and parse this JSON formatted response。
在此先感谢,
这是基于调度和JSON4s
import dispatch._
import Defaults._
import org.json4s._
import org.json4s.jackson.JsonMethods._
val translateAPI = url("https://www.googleapis.com/language/translate/v2/")
val response = Http(translateAPI OK as.String)
val json = parse(response()) //() is added by Dispatch and forces to await the result forever == Await.result(response , forever)
要得到你需要将以下添加到您的构建文件库的例子。 :示例SBT
libraryDependencies += "org.json4s" %% "json4s-jackson" % "3.2.8"
libraryDependencies += "net.databinder.dispatch" %% "dispatch-core" % "0.11.0"
你在例子中的URL参数缺乏和凭据没有这些,你会得到一个错误400-。但如果你能解决这个问题,它应该可以工作。
是的,我试过我在。,spray-client和Dispath中都得到了超时异常,我通过浏览器的URI检查它的工作正常,后来我意识到我的程序运行在代理之后,我该如何克服这种情况 – BalaB
看看http://stackoverflow.com/questions/14253515/use-dispatch-0-9-5-behind-proxy那里解决了这个问题。 –
这里是基于褴褛的客户
val client = ScruffyClient()
val resp = client.prepareGet("https://www.googleapis.com/language/translate/v2").execute()
val entityAsString = resp.bodyAsString
val marshalledJson = resp.bodyAs[MyCaseClass]
替代例子,你需要输入:
libraryDependencies += "com.sksamuel.scruffy" %% "scruffy-client" % "1.2.5"
你尝试过这么远吗?尝试逐步解决个人问题。你有什么资源关于HTTP请求,什么做了/没有工作? – reto
我试过Dispatch框架,它甚至没有帮我尝试添加Thread.Sleep。但没有解决。 :( – BalaB
我建议这个库的HTTP GET:https://github.com/scalaj/scalaj-http 和这个:https://github.com/json4s/json4s JSON操作。 – liosedhel