从URL读取总是返回NULL

时间:2018-12-12 20:20:04

标签: java json url inputstream

因此,我尝试了几种解决方案,但结果始终相同。我从随机生成的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();

}

0 个答案:

没有答案