Java Http request on api.stackexchange with json response -
i'm trying use api-stackexchange java when request , try parse response json parser have error.
public arraylist<question> readjsonstream(inputstream in) throws ioexception { jsonreader reader = new jsonreader(new inputstreamreader(in, "utf-8")); reader.setlenient(true); try { system.out.println(reader.nextstring()); // � special character return readitem(reader); } { reader.close(); } } public arraylist<question> readitem(jsonreader reader) throws ioexception { arraylist<question> questions = new arraylist<question>(); reader.beginobject(); while (reader.hasnext()) { system.out.println("here");//not print error before string name = reader.nextname(); if (name.equals("items")) { questions = readquestionsarray(reader); } } reader.endobject(); return questions; } public final static void main(string[] args) throws exception { uribuilder builder = new uribuilder(); builder.setscheme("http").sethost("api.stackexchange.com").setpath("/2.0/search") .setparameter("site", "stackoverflow") .setparameter("intitle" ,"workaround") .setparameter("tagged","javascript"); uri uri = builder.build(); string surl = fixencoding(uri.tostring()+"&filter=!)qwra9i-can0pqguwq7)dvtm"); system.out.println(surl); test t = new test(); try { url url = new url(surl); t.readjsonstream(url.openstream()); } catch (ioexception e) { e.printstacktrace(); } } and error is:
com.google.gson.stream.malformedjsonexception: expected literal value @ line 1 column 19
here example of json :
{ "items": [ { "question_id": 10842231, "score": 0, "title": "how push oath token localstorage or localsession , listen storage event? (soundcloud php/js bug workaround)", "tags": [ "javascript", "javascript-events", "local-storage", "soundcloud" ], "answers": [ { "question_id": 10842231, "answer_id": 10857488, "score": 0, "is_accepted": false } ], "link": "http://stackoverflow.com/questions/10842231/how-to-push-oath-token-to-localstorage-or-localsession-and-listen-to-the-storage", "is_answered": false },... here url of request:
so what's problem? json malformed? or did not right?
thanks, anthony
edit:
i'm sure problem come request, paste response of request via browser in text file host in server apache , works fine. abble parse json of response.
change code:
if (name.equals("items")) { questions = readquestionsarray(reader); } to code:
if (name.equals("items")) { questions = readquestionsarray(reader); } else { reader.skipvalue(); } otherwise end calling nextname() twice in row, invalid.
Comments
Post a Comment