擊中我下面的代碼例外在URIJSONTokener:一個JSONObject文本必須以 '{'
public static String processRestResponse(String language){
URI uri = null;
JSONTokener tokener = null;
try {
uri = new URI("https://api.github.com/search/repositories?q=language:Java");
URL url = uri.toURL();
InputStream inputStream = url.openStream();
tokener = new JSONTokener(inputStream.toString());
JSONObject root = new JSONObject(tokener);
} catch (Exception e) {
e.printStackTrace();
}
異常使用JSON如下...
org.json.JSONException: A JSONObject text must begin with '{' at character 1
at org.json.JSONTokener.syntaxError(JSONTokener.java:410)
at org.json.JSONObject.<init>(JSONObject.java:179)
at Assignment1.processRestResponse(Assignment1.java:48)
at Assignment1.main(Assignment1.java:108)
是否有其他我可以採取適合的方法嗎?
'inputStream.toString()'很肯定這不是你從'InputStream'中讀取的方式。 –
InputStream toString不會給你正確的響應。檢查如何從inputStream中讀取 –