带有http客户端调度库的Scala工作表

时间:2014-08-16 20:07:12

标签: scala xml-parsing httpclient scala-2.10

我正在努力学习scala,在访问ReST api时遇到了一些障碍。

以下是该项目的依赖项:

libraryDependencies ++= Seq(
  "net.databinder.dispatch" %% "dispatch-core" % "0.11.0"
)  

这是我目前在scala工作表中使用的代码。

import dispatch._, Defaults._

val request = url(s"http://freegeoip.net/xml/www.google.com")


val busTime = Http( request <<? Map("key"->apiKey) OK as.xml.Elem)


def extractTime(xml: scala.xml.Elem) =
    for {
        response <- xml \\ "Response"
    } yield xml

busTime()

但是,当我尝试按照我在scala上的书中列出的方式解析xml表达式时,我得到以下异常,我无法弄清楚原因。

java.util.concurrent.ExecutionException: java.lang.NoSuchMethodError: scala.collection.immutable.Range.validateRangeBoundaries(Lscala/Function1;)Z
    at com.ning.http.client.providers.netty.NettyResponseFuture.done(experiment.sc8852822906338575612.tmp:321)
    at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.markAsDone(experiment.sc8852822906338575612.tmp:1427)
    at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.finishUpdate(experiment.sc8852822906338575612.tmp:1443)
    at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.access$2500(experiment.sc8852822906338575612.tmp:149)
    at com.ning.http.client.providers.netty.NettyAsyncHttpProvider$HttpProtocol.handle(experiment.sc8852822906338575612.tmp:2195)
    at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.messageReceived(experiment.sc8852822906338575612.tmp:1150)
    at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(experiment.sc8852822906338575612.tmp:66)
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(experiment.sc8852822906338575612.tmp:560)
    at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(experiment.sc8852822906338575612.tmp:787)
    at org.jboss.netty.handler.stream.ChunkedWriteHandler.handleUpstream(experiment.sc8852822906338575612.tmp:138)
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(experiment.sc8852822906338575612.tmp:560)
    at #worksheet#.#worksheet#(experiment.sc8852822906338575612.tmp:787)
Caused by: java.lang.NoSuchMethodError: scala.collection.immutable.Range.validateRangeBoundaries(Lscala/Function1;)Z
    at scala.xml.parsing.FactoryAdapter.startElement(FactoryAdapter.scala:131)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:506)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(XMLDTDValidator.java:745)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1323)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$ContentDriver.scanRootElementHook(XMLDocumentScannerImpl.java:1277)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3065)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:881)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:489)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:302)
    at scala.xml.factory.XMLLoader$class.loadXML(XMLLoader.scala:40)
    at scala.xml.XML$$anon$1.loadXML(XML.scala:68)
    at scala.xml.factory.XMLLoader$class.loadString(XMLLoader.scala:59)
    at scala.xml.XML$$anon$1.loadString(XML.scala:68)
    at dispatch.as.xml.Elem$.apply(elem.scala:8)
    at dispatch.as.xml.Elem$.apply(elem.scala:6)

陌生人的部分是当我将响应解析为一个String时,它显示有效的xml。虽然这只是一个不符合我想要的字符串,但能够使用上述功能会很棒。

感谢任何帮助,谢谢!

更新

根据评论我想在sbt repl中尝试它,结果如下:

scala> val request = url(s"http://freegeoip.net/xml/www.google.com")
request: dispatch.Req = Req(<function1>)

scala> var response = Http( request OK as.xml.Elem)
response: dispatch.Future[scala.xml.Elem] = scala.concurrent.impl.Promise$DefaultPromise@7fd2e7a

scala> response()
res2: scala.xml.Elem =
<Response>
    <Ip>173.194.64.106</Ip>
    <CountryCode>US</CountryCode>
    <CountryName>United States</CountryName>
    <RegionCode>CA</RegionCode>
    <RegionName>California</RegionName>
    <City>Mountain View</City>
    <ZipCode>94043</ZipCode>
    <Latitude>37.4192</Latitude>
    <Longitude>-122.0574</Longitude>
    <MetroCode>807</MetroCode>
    <AreaCode>650</AreaCode>
</Response>

scala> def extractTime(xml: scala.xml.Elem) =
     |     for {
     |         response <- xml \\ "Response"
     |     } yield xml
extractTime: (xml: scala.xml.Elem)scala.xml.NodeSeq

scala> extractTime(response())
res3: scala.xml.NodeSeq =
NodeSeq(<Response>
    <Ip>173.194.64.106</Ip>
    <CountryCode>US</CountryCode>
    <CountryName>United States</CountryName>
    <RegionCode>CA</RegionCode>
    <RegionName>California</RegionName>
    <City>Mountain View</City>
    <ZipCode>94043</ZipCode>
    <Latitude>37.4192</Latitude>
    <Longitude>-122.0574</Longitude>
    <MetroCode>807</MetroCode>
    <AreaCode>650</AreaCode>
</Response>)

所以现在我的新问题是为什么这组命令在repl中工作但不在工作表中?

0 个答案:

没有答案