我是一名Groovy新手,並嘗試從Groovy腳本連接到GitHub API,但是當HTTPBuilder嘗試解析JSON響應時出現奇怪的錯誤。Groovy HTTPBuilder在解析有效的json時產生錯誤
我的簡單腳本如下
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.2')
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
def apiToken = '[api_token]'
def http = new HTTPBuilder('https://api.github.com/')
// perform a GET request, expecting JSON response data
http.request(GET, JSON) {
uri.path = '/orgs/octokit/repos'
headers.'Accept' = 'application/json'
headers.'Authorization' = 'Basic ' + (apiToken + ':').bytes.encodeBase64().toString()
headers.'User-Agent' = '[username]'
// response handler for a success response code:
response.success = { resp, json ->
println resp.statusLine
}
// handler for any failure status code:
response.failure = { resp ->
println "Unexpected error: ${resp.statusLine.statusCode} : ${resp.statusLine.reasonPhrase}"
}
}
但當JSON解析我收到以下錯誤
Sep 06, 2014 10:21:54 PM groovyx.net.http.HTTPBuilder$1 handleResponse
WARNING: Error parsing 'application/json; charset=utf-8' response
groovy.json.JsonException: expecting '}' or ',' but got current char '"' with an int value of 34
The current character read is '"' with an int value of 34
expecting '}' or ',' but got current char '"' with an int value of 34
line number 1
index number 256
[{"id":417862,"name":"octokit.rb","full_name":"octokit/octokit.rb","owner":{"login":"octokit","id":3430433,"avatar_url":"https://avatars.githubusercontent.com/u/3430433?v=2","gravatar_id":"43f38795089d56a2a7092b7d0c71fa76","url":"h
................................................................................................................................................................................................................................................................^
at groovy.json.internal.JsonParserCharArray.complain(JsonParserCharArray.java:162)
at groovy.json.internal.JsonParserCharArray.decodeJsonObject(JsonParserCharArray.java:152)
at groovy.json.internal.JsonParserCharArray.decodeValueInternal(JsonParserCharArray.java:196)
at groovy.json.internal.JsonParserCharArray.decodeJsonObject(JsonParserCharArray.java:140)
at groovy.json.internal.JsonParserCharArray.decodeValueInternal(JsonParserCharArray.java:196)
at groovy.json.internal.JsonParserCharArray.decodeJsonArray(JsonParserCharArray.java:346)
at groovy.json.internal.JsonParserCharArray.decodeValueInternal(JsonParserCharArray.java:192)
at groovy.json.internal.JsonParserCharArray.decodeValue(JsonParserCharArray.java:166)
at groovy.json.internal.JsonParserCharArray.decodeFromChars(JsonParserCharArray.java:45)
at groovy.json.internal.JsonParserCharArray.parse(JsonParserCharArray.java:409)
at groovy.json.internal.BaseJsonParser.parse(BaseJsonParser.java:121)
at groovy.json.JsonSlurper.parse(JsonSlurper.java:224)
at groovyx.net.http.ParserRegistry.parseJSON(ParserRegistry.java:280)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1207)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1074)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1016)
at groovy.lang.Closure.call(Closure.java:423)
at groovy.lang.Closure.call(Closure.java:439)
at groovyx.net.http.HTTPBuilder.parseResponse(HTTPBuilder.java:560)
at groovyx.net.http.HTTPBuilder$1.handleResponse(HTTPBuilder.java:489)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1070)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1044)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:515)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:434)
at groovyx.net.http.HTTPBuilder.request(HTTPBuilder.java:383)
at groovyx.net.http.HTTPBuilder$request.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:124)
at sprint-snapshot.run(sprint-snapshot.groovy:11)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:258)
at groovy.lang.GroovyShell.run(GroovyShell.java:502)
at groovy.lang.GroovyShell.run(GroovyShell.java:491)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:650)
at groovy.ui.GroovyMain.run(GroovyMain.java:381)
at groovy.ui.GroovyMain.process(GroovyMain.java:367)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:126)
at groovy.ui.GroovyMain.main(GroovyMain.java:106)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:106)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:128)
Caught: groovyx.net.http.ResponseParseException: OK
groovyx.net.http.ResponseParseException: OK
at groovyx.net.http.HTTPBuilder$1.handleResponse(HTTPBuilder.java:495)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1070)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1044)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:515)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:434)
at groovyx.net.http.HTTPBuilder.request(HTTPBuilder.java:383)
at groovyx.net.http.HTTPBuilder$request.call(Unknown Source)
at sprint-snapshot.run(sprint-snapshot.groovy:11)
Caused by: groovy.json.JsonException: expecting '}' or ',' but got current char '"' with an int value of 34
The current character read is '"' with an int value of 34
expecting '}' or ',' but got current char '"' with an int value of 34
line number 1
index number 256
[{"id":417862,"name":"octokit.rb","full_name":"octokit/octokit.rb","owner":{"login":"octokit","id":3430433,"avatar_url":"https://avatars.githubusercontent.com/u/3430433?v=2","gravatar_id":"43f38795089d56a2a7092b7d0c71fa76","url":"h
................................................................................................................................................................................................................................................................^
at groovyx.net.http.ParserRegistry.parseJSON(ParserRegistry.java:280)
at groovyx.net.http.HTTPBuilder.parseResponse(HTTPBuilder.java:560)
at groovyx.net.http.HTTPBuilder$1.handleResponse(HTTPBuilder.java:489)
... 7 more
錯誤似乎暗示無效JSON但我已確認的響應的確正如您對Github API期望的那樣有效。
更奇怪的是,如果我完全刪除了「授權」報頭和發送請求未經身份驗證,它工作正常:S
保存響應JSON和嘗試解析它Groovy的JSON解析器 – injecteer 2014-09-06 21:47:59
請你提供JSON響應你越來越了? – Opal 2014-09-07 12:32:07
提供的代碼對我來說工作正常。 – Opal 2014-09-07 12:36:58