请求后JSON解析器Java

请求后JSON解析器Java,java,json,post,Java,Json,Post,我读过这篇关于如何用Java做post请求的文章。我不明白如何在JSON解析器中实现这一点。这就是我迄今为止所尝试的: public class JSONParser { private String read(BufferedReader bufferedReader) throws IOException { //Creates new StringBuilder to avoid escaping chars StringBuilder string

我读过这篇关于如何用Java做post请求的文章。我不明白如何在JSON解析器中实现这一点。这就是我迄今为止所尝试的:

public class JSONParser {
    private String read(BufferedReader bufferedReader) throws IOException {
        //Creates new StringBuilder to avoid escaping chars
        StringBuilder stringBuilder = new StringBuilder();
        //Gets the currentLine
        String currentLine;
        while((currentLine = bufferedReader.readLine()) !=null ){
            //Adds the currentLine to the stringBuild if the currentLine is not null
            stringBuilder.append(currentLine);
        }
        //Returns the StringBuilder is String format
        return stringBuilder.toString();
    }

    public JSONObject readJsonFromUrl(String JSONurl) throws IOException, JSONException {
        InputStream url = new URL(JSONurl).openStream();
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url));
            String jsonText = read(bufferedReader);
            JSONObject json = new JSONObject(jsonText);
            return json;
        } finally {
            url.close();
        }
    }

    public void printJSON() throws IOException, JSONException {
        JSONObject json = readJsonFromUrl("http://grwn.ddns.net:1337/locations");
        System.out.print(json);

        //for (Integer i = 0; i < json.getJSONArray("damage_or_theft_car").length(); i++) {
        //    System.out.println(json.getJSONArray("damage_or_theft_car")
        //.getJSONObject(i).get("hood_id"));
        //}
    }
}

有人能帮我解决问题或给我指出正确的方向吗?

导致此问题的可能原因可能是以下原因之一; 1无需从url读取/获取任何内容 2检查代理设置,可能是在代理上配置的用于访问上述url的内容 主机上缺少3个主机名映射

手动验证的一些方法如下:; 1尝试使用代理设置从web浏览器访问url,查看是否可以获得所需的原始json 2尝试在没有代理设置的情况下从web浏览器访问url,查看是否可以获得所需的原始json
3如果第1步或第2步成功,请从java代码中尝试相同的操作。我认为您需要指定在打开连接后要发出POST请求,可以尝试类似的操作

public JSONObject readJsonFromUrl(String JSONurl) throws IOException, JSONException {
    URL url = new URL(JSONurl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProprtry("Content-type", "application/JSON");
    try {
        BufferedReader bufferedReader = new BufferedReader(conn. getInputStream());
        String jsonText = read(bufferedReader);
        JSONObject json = new JSONObject(jsonText);
        return json;
    } finally {
        url.close();
    }
}

获取FileNotFoundE的原因可能是响应代码为404。您是否尝试过其他不会导致404响应的post请求?@Semyow它不会发送post请求,因为我不知道BufferReader如何不能应用于java.io.InputStream您可以创建一个inputStreamReader实例,然后像您一样创建一个BufferReader。我不能测试它,因为我在电话自动取款机上。我已经修好了。我会马上编辑你的答案并接受它!
public JSONObject readJsonFromUrl(String JSONurl) throws IOException, JSONException {
    URL url = new URL(JSONurl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProprtry("Content-type", "application/JSON");
    try {
        BufferedReader bufferedReader = new BufferedReader(conn. getInputStream());
        String jsonText = read(bufferedReader);
        JSONObject json = new JSONObject(jsonText);
        return json;
    } finally {
        url.close();
    }
}