Scala 和 HttpClient:如何解决此错误?

发布于 2024-08-29 12:58:02 字数 1440 浏览 4 评论 0原文

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野生奥特曼 2024-09-05 12:58:02

我也必须处理这个问题。尝试如下操作:

val handler:ResponseHandler[String] = new BasicResponseHandler
val request = new HttpGet("...")
val response = client execute request
val body = handler handleResponse response

这在 2.7.7 中对我来说效果很好。它只有 1 条额外的线,所以还不错。

Ive had to deal with this as well. Try something like the following:

val handler:ResponseHandler[String] = new BasicResponseHandler
val request = new HttpGet("...")
val response = client execute request
val body = handler handleResponse response

This works fine for me in 2.7.7. Its only 1 extra line, so not too bad.

岁月打碎记忆 2024-09-05 12:58:02

对于 Scala 2.7,有报道称(2016),但收效甚微。也许人们可以重新打开它,给出一个更容易重现的案例。

您可以使用反射(通过 Scala 的结构类型)来解决此问题,如下所示:

import org.apache.http.client._
import org.apache.http.client.methods._
import org.apache.http.impl.client._
val httpclient = new DefaultHttpClient
val httpget = new HttpGet("www.google.com")
val brh = new BasicResponseHandler[String]
//httpclient.execute (httpget, brh)
httpclient.asInstanceOf[{
  def execute (request: HttpUriRequest,
               responseHandler: ResponseHandler[String]): String
}].execute (httpget, brh)

在 Scala 2.8 中,我发现以下代码可以工作:

import org.apache.http.client._
import org.apache.http.client.methods._
import org.apache.http.impl.client._
val httpclient = new DefaultHttpClient
val httpget = new HttpGet("www.google.com")
val brh = new BasicResponseHandler
httpclient.execute (httpget, brh)

因此我认为它已在 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:

import org.apache.http.client._
import org.apache.http.client.methods._
import org.apache.http.impl.client._
val httpclient = new DefaultHttpClient
val httpget = new HttpGet("www.google.com")
val brh = new BasicResponseHandler[String]
//httpclient.execute (httpget, brh)
httpclient.asInstanceOf[{
  def execute (request: HttpUriRequest,
               responseHandler: ResponseHandler[String]): String
}].execute (httpget, brh)

With Scala 2.8 I have found that the following code works:

import org.apache.http.client._
import org.apache.http.client.methods._
import org.apache.http.impl.client._
val httpclient = new DefaultHttpClient
val httpget = new HttpGet("www.google.com")
val brh = new BasicResponseHandler
httpclient.execute (httpget, brh)

Therefore I think it is fixed in 2.8.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文