2

我对GroovyHttpBuilder库有奇怪的问题。首先要注意,我对 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 的新手,我很难找出发生了什么,所以请帮忙。

4

1 回答 1

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 版本的库开始就存在于类中。

于 2011-05-19T08:04:33.667 回答