Scala - send example not working
I am following the first example in the dispatch docs -
val svc = url("http://api.hostip.info/country.php")
val country = Http(svc OK as.String)
for (c <- country)
println(c)
I am not getting printed output. When I change it below to make a blocking call, I get the output.
val res = country()
println(res)
Need help with this.
Full program -
import dispatch._
object DispatchTest {
def main(args: Array[String]) {
val svc = url("http://api.hostip.info/country.php")
val country = Http(svc OK as.String)
for (c <- country)
println(c)
}
}
source to share
Hmm, not sure here, but maybe the problem is that your main thread finished so quickly that the background thread (in which Dispatch is running asynchronously) has no time to take action?
To test this, you can try inserting a delay:
for (c <- country) // Here we spawn a background thread!
println(c)
Thread.sleep(500) // So let wait half a second for it to work
Of course, in a real program, you never have to do this.
Another delay option is simply readLine()
at the end of the main one.
source to share
He works here:
scala> import dispatch._
import dispatch._
scala> val svc = url("http://api.hostip.info/country.php")
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
svc: com.ning.http.client.RequestBuilder = com.ning.http.client.RequestBuilder@2f823290
scala> val country = Http(svc OK as.String)
country: dispatch.Promise[String] = Promise(-incomplete-)
scala> for (c <- country)
| println(c)
scala> BR
Please note that that BR
appeared after the hint.
Are you sure the country was not printed somewhere, but you didn't notice it because it was confused with a different output?
source to share