Groovy HTTPBuilder Herstellung von Fehler beim Parsen gültiges json

Ich bin ein Groovy-Anfänger und versuche zu verbinden, um die GitHub-API von einem groovy-Skript aber ich erhalte eine seltsame Fehlermeldung, wenn HTTPBuilder versucht zum Parsen der JSON-Antwort.

Mein einfaches Skript ist wie folgt

@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}"
  }
}

Aber wenn die JSON geparst bekomme ich die folgende Fehlermeldung

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

Den Fehler zu suggerieren scheint, ungültige JSON-ich habe jedoch bestätigt die Antwort ist in der Tat gültig, als Sie erwarten würden von der Github API.

Was ist noch seltsamer ist, wenn ich entfernen Sie die 'Authorization' - header vollständig aus und senden die Anfrage nicht authentifiziert, funktioniert es einwandfrei :S

  • speichern Sie die json-Antwort und versuchen Sie zu analysieren, es groovy ist json-parser
  • Könnten Sie bitte geben Sie die JSON-Antwort, die Sie bekommen?
  • Der code funktioniert gut für mich.
InformationsquelleAutor 3urdoch | 2014-09-06
Schreibe einen Kommentar