我尝试从HTTPBuilder的URL中获取JSON,但是这段代码返回一个异常,返回这个erro“方法抛出'groovy.lang.MissingPropertyException‘异常”。
static request(String path, boolean isGet){
def myClient = new HTTPBuilder("${HOST}${path}")
def jsonResp = [:]
try{
if(isGet){
log.info "[EXAMPLE GET] ${HOST}${path}"
myClient.get(requestContentType: ContentType.JSON){ resp, json ->
jsonResp = resp
}
}
}catch(Exception e){
println "erro: "
log.info "[EXAMPLE ERROR]: ${e.message}"
println(e.message)
}
jsonResp
}发布于 2017-03-31 20:22:39
MissingPropertyException是开什么的?
我在一个脚本中尝试了您的示例(有几个mods),这次返回json而不是resp,这似乎很有效。
import groovy.transform.Field
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
@Field final String HOST = 'http://echo.jsontest.com'
def resp = request( "/name/jon", true )
println resp.toString()
def request(String path, boolean isGet){
def myClient = new HTTPBuilder("${HOST}${path}")
def jsonResp = [:]
try{
if(isGet){
println "[EXAMPLE GET] ${HOST}${path}"
myClient.get(requestContentType: ContentType.JSON){ resp, json ->
jsonResp = json
}
}
}
catch(Exception e) {
println(e.message)
}
jsonResp}
产出如下:
{"name":"jon"}https://stackoverflow.com/questions/43145676
复制相似问题