我在Apache HttpClient中使用scala,并通过示例进行操作。我收到以下错误:
/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中成功运行该示例...
答案 0 :(得分:2)
我也必须处理这个问题。尝试以下内容:
val handler:ResponseHandler[String] = new BasicResponseHandler
val request = new HttpGet("...")
val response = client execute request
val body = handler handleResponse response
这在2.7.7中适合我。它只有一条额外的线,所以也不会太糟糕。
答案 1 :(得分:1)
对于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。