繁体   English   中英

带有JSON响应的api.stackexchange上的Java Http请求

[英]Java Http request on api.stackexchange with json response

我正在尝试将api-stackexchange与java一起使用,但是当我执行请求并尝试使用json解析器解析响应时,我遇到了错误。

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();
    }

}

错误是:

com.google.gson.stream.MalformedJsonException:第1行第19列的预期文字值

这是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
            },...

这是请求的URL:

https://api.stackexchange.com/2.0/search?tagged=javascript&intitle=workaround&site=stackoverflow&filter=!)QWRa9I-CAn0PqgUwq7)DVTM

所以有什么问题? 杰森真的畸形吗? 还是我做错了什么?

谢谢安东尼

编辑:

现在,我确定问题出在请求上,我通过浏览器将请求的响应粘贴到服务器Apache中承载的文本文件中,并且工作正常。 我很想解析回应的Json。

更改此代码:

    if (name.equals("items")) {
        questions = readQuestionsArray(reader);
    }

此代码:

    if (name.equals("items")) {
        questions = readQuestionsArray(reader);
    } else {
        reader.skipValue();
    }

否则,您最终将连续两次调用nextName() ,这是无效的。

响应中的数据使用deflate算法压缩。 因此,我使用GZIPInputStream封装了InputStream:

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();
ArrayList<Question> q =null;
String result = "";
String surl = fixEncoding(uri.toString()+"&filter=!)QWRa9I-CAn0PqgUwq7)DVTM");
System.out.println(surl);
Test t = new Test();

    try {
    URL url = new URL(surl);
    q = t.readJsonStream(new GZIPInputStream(url.openStream()));        
} 

    catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println(result);

    for (Question question : q) {
        System.out.println(question.title);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM