因此,我尝试了几种解决方案,但结果始终相同。我从随机生成的JSON文件(www.json-generator.com)中读取,并且BufferedReader行始终抛出NullPointerException。 这是我的代码:
public JSONArray readJsonFromUrl(String url) throws IOException, JSONException {
URLConnection uc = new URL(JSONurl).openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream())
String jsonText = readAll(in);
JSONArray json = new JSONArray(jsonText);
return json;
}
//parse the json
public String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int counter;
while((counter=rd.read())!=-1)
sb.append((char)counter);
return sb.toString();
}