Java Http request on api.stackexchange with json response

Posted by taoder on Stack Overflow See other posts from Stack Overflow or by taoder
Published on 2012-06-04T16:29:08Z Indexed on 2012/06/04 16:42 UTC
Read the original article Hit count: 290

Filed under:
|
|
|

I'm trying to use the api-stackexchange with java but when I do the request and try to parse the response with a json parser I have an 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);
    } finally {
        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 the error is 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 the error is:

com.google.gson.stream.MalformedJsonException: Expected literal value at line 1 column 19

Here is an example of the Json :

        {
          "items": [
            {
              "question_id": 10842231,
              "score": 0,
              "title": "How to push oath token to LocalStorage or LocalSession and listen to the 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
            },...

So what's the problem? Is the Json really malformed? Or did I do something not right?

Thanks, Anthony

© Stack Overflow or respective owner

Related posts about java

Related posts about JSON