gpt4 book ai didi

Scala 和 HttpClient : How do I resolve this error?

转载 作者:行者123 更新时间:2023-12-03 18:41:03 25 4
gpt4 key购买 nike

我将 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 中成功运行示例...

最佳答案

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

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

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

关于Scala 和 HttpClient : How do I resolve this error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2694031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com