Scala and HttpClient: how to fix this error?

I use scala with Apache HttpClient and work through examples. I get 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 highlighted error and line:

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 ...

+3
source share
2 answers

I also had to deal with this. Try 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 so bad.

+2
source

For Scala 2.7, it was reported ( 2016 ), but with little success. Maybe you can open it again, giving a case that is easier to reproduce.

( 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.

+1

Source: https://habr.com/ru/post/1742340/


All Articles