Scala 和 HttpClient:如何解决此错误?
我将 scala 与 Apache HttpClient 结合使用,并通过示例进行工作。我收到以下错误:
/Users/name/IdeaProjects/JakartaCapOne/src/JakExamp.scala
Error:Error:line (16)error: overloaded method value execute with alternatives
(org.apache.http.HttpHost,org.apache.http.HttpRequest)org.apache.http.HttpResponse
<and>
(org.apache.http.client.methods.HttpUriRequest,org.apache.http.protocol.HttpContext)org.apache.http.HttpResponse
cannot be applied to
(org.apache.http.client.methods.HttpGet,org.apache.http.client.ResponseHandler[String])
val responseBody = httpclient.execute(httpget, responseHandler)
这是突出显示错误和相关行的代码:
import org.apache.http.client.ResponseHandler
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.BasicResponseHandler
import org.apache.http.impl.client.DefaultHttpClient
object JakExamp {
def main(args : Array[String]) : Unit = {
val httpclient: HttpClient = new DefaultHttpClient
val httpget: HttpGet = new HttpGet("www.google.com")
println("executing request..." + httpget.getURI)
val responseHandler: ResponseHandler[String] = new BasicResponseHandler
val responseBody = httpclient.execute(httpget, responseHandler)
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
println(responseBody)
client.getConnectionManager.shutdown
}
}
我可以在 java 中成功运行该示例...
I'm using scala with Apache HttpClient, and working through examples. I'm getting the following error:
/Users/name/IdeaProjects/JakartaCapOne/src/JakExamp.scala
Error:Error:line (16)error: overloaded method value execute with alternatives
(org.apache.http.HttpHost,org.apache.http.HttpRequest)org.apache.http.HttpResponse
<and>
(org.apache.http.client.methods.HttpUriRequest,org.apache.http.protocol.HttpContext)org.apache.http.HttpResponse
cannot be applied to
(org.apache.http.client.methods.HttpGet,org.apache.http.client.ResponseHandler[String])
val responseBody = httpclient.execute(httpget, responseHandler)
Here is the code with the error and line in question highlighted:
import org.apache.http.client.ResponseHandler
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.BasicResponseHandler
import org.apache.http.impl.client.DefaultHttpClient
object JakExamp {
def main(args : Array[String]) : Unit = {
val httpclient: HttpClient = new DefaultHttpClient
val httpget: HttpGet = new HttpGet("www.google.com")
println("executing request..." + httpget.getURI)
val responseHandler: ResponseHandler[String] = new BasicResponseHandler
val responseBody = httpclient.execute(httpget, responseHandler)
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
println(responseBody)
client.getConnectionManager.shutdown
}
}
I can successfully run the example in java...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也必须处理这个问题。尝试如下操作:
这在 2.7.7 中对我来说效果很好。它只有 1 条额外的线,所以还不错。
Ive had to deal with this as well. Try something like the following:
This works fine for me in 2.7.7. Its only 1 extra line, so not too bad.
对于 Scala 2.7,有报道称(2016),但收效甚微。也许人们可以重新打开它,给出一个更容易重现的案例。
您可以使用反射(通过 Scala 的结构类型)来解决此问题,如下所示:
在 Scala 2.8 中,我发现以下代码可以工作:
因此我认为它已在 2.8 中修复。
For Scala 2.7 it was reported (2016) but with little success. Maybe one can reopen it giving a case wich is easier to reproduce.
You can use reflection (via Scala's structural typing) to workaround this, as follows:
With Scala 2.8 I have found that the following code works:
Therefore I think it is fixed in 2.8.