2012-06-04 165 views
0

我想用java的api-stackexchange但当我做请求并尝试用json解析器解析响应我有一个错误。api.stackexchange与json响应的Java Http请求

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

那么,有什么问题呢? Json真的变形了吗?或者我做了什么不对吗?

感谢,安东尼

编辑:

我现在肯定,问题来的要求,我通过粘贴在一个文本文件浏览器,我在一个服务器主机请求的响应Apache和它工作正常。我有能力解析答复的Json。

回答

2

响应中的数据使用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); 
    } 
} 
2

更改此代码:

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

这个代码:

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

否则,你最终成一排,这是无效的调用nextName()两次。

+0

顺便说一句,我是GSON维护者之一,你给我造成了恐慌对这个问题还真不少。我花了十分钟才弄清楚问题所在! –

+0

谢谢,但这不能解决我的问题。我认为问题在于http请求。反应可能不是一个良好形成的Json。为什么reader.nextString()会给我特殊的字符,而不是真正的下一个字符串。 – AnthonyMaia

+0

它是BOM吗? http://blog.publicobject.com/2010/08/handling-byte-order-mark-in-java.html –