我对 Groovy 和 HttpBuilder 库有一些奇怪的问题。首先要注意的是,我对Groovy很新鲜。
我的代码基于教程。它只是从HTTP服务器加载文件列表。代码工作昨天,今天(工作区构建后)没有。
问题是:
Caught: groovy.lang.MissingMethodException: No signature of method: groovyx.net.http.HTTPBuilder.request() is applicable for argument types: (groovyx.net.http.Method, groovyx.net.http.ContentType, pl.linfo.groovy.samples.HttpTest$_main_closure1)
Possible solutions: request(groovyx.net.http.Method, groovy.lang.Closure)
代码是:
def http = new HTTPBuilder( 'http://nbp.pl/Kursy/xml/dir.txt' )
http.request( GET, TEXT ) {
response.success = { resp, reader ->
println "${resp.statusLine}"
files = reader.text.split ('\r\n')
}
response.'404' = {
println "Not found!"
return
}
};
运行环境 Eclipse 3.6
我认为问题是groovy编译问题,重新编译后的groovy代码片段不再匹配Closure。但是,作为Groovy的新手,我有问题要找出发生了什么,所以请帮助。
答案 0 :(得分:1)
这是Eclipse Groovy插件的一些问题。使用groovy解释器运行时,您发布的代码对我来说很有用。
$ cat hbuildertest.groovy
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.1' )
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
def http = new HTTPBuilder( 'http://nbp.pl/Kursy/xml/dir.txt' )
http.request( GET, TEXT ) {
response.success = { resp, reader ->
println "${resp.statusLine}"
files = reader.text.split ('\r\n')
}
response.'404' = {
println "Not found!"
return
}
};
$ groovy hbuildertest.groovy
May 19, 2011 12:59:08 AM groovyx.net.http.ParserRegistry getCharset
WARNING: Could not find charset in response
HTTP/1.1 200 OK
$
还有签名的方法:
public Object request( Method m, Object contentType, Closure configClosure )
throws ClientProtocolException, IOException
存在于groovyx.net.http.HTTPBuilder
类中,因为至少有0.3.0版本的库。