POST请求JSON Parser Java

时间:2017-06-28 08:48:24

标签: java json post

POST Request Java

我已经阅读过有关如何使用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"));
        //}
    }
}

当我使用不需要POST请求的链接运行此代码时,一切正常,但是当我在需要POST请求的链接上运行此代码时,我收到以下错误:

Exception in thread "main" java.io.FileNotFoundException: http://grwn.ddns.net:1337/locations
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1872)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
    at java.net.URL.openStream(URL.java:1045)
    at com.company.JSONParser.readJsonFromUrl(JSONParser.java:30)
    at com.company.JSONParser.printJSON(JSONParser.java:42)
    at com.company.Main.main(Main.java:33)

有人可以帮助我或指出我正确的方向吗?

2 个答案:

答案 0 :(得分:1)

我认为你需要在打开连接后指定要发出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();
    }
}

答案 1 :(得分:0)

此问题的可能原因可能是以下之一; 1)没有什么可以从网址读取/获取 2)检查代理设置,可能是代理上配置的用于访问上述URL的内容 3)主机上缺少主机名映射

手动验证此方法的一些方法如下; 1)尝试使用代理设置从Web浏览器访问URL,看看是否可以获得所需的原始json 2)尝试从Web浏览器访问URL而不进行代理设置,看看是否可以获得所需的原始json 3)如果步骤1)或步骤2)成功,请从您的Java代码中尝试相同