首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在Java HTTP POST请求中发送JSON

无法在Java HTTP POST请求中发送JSON
EN

Stack Overflow用户
提问于 2019-06-05 22:53:22
回答 1查看 841关注 0票数 0

我收到了一个‘服务器返回的HTTP响应代码: 500’错误,尽管我已经检查了我发送的内容(我甚至尝试使用在线工具发送它,但它工作了)。API密钥和JSON是正确的。当我试图用'connection.getInputStream()‘读取输入流时,我得到了这个错误。这是从哪里来的呢?我是不是忘了什么?我正在尝试通过openrouteservice实现这个特性:https://openrouteservice.org/dev/#/api-docs/v2/directions/{/post}

代码语言:javascript
复制
    public static UPSRoute getRoute(Location start, Location end, String language) {
        if (language.equals("fr")) {
            JSONObject jsonObject = null;

            try {
                URL url = new URL("https://api.openrouteservice.org/v2/directions/foot-walking");
                String payload = "{\"coordinates\":[[" + start.getCoordinates() + "],[" + end.getCoordinates() + "]],\"language\":\"fr\"}";
                System.out.println(payload); //{"coordinates":[[1.463478,43.562038],[1.471717,43.560787]],"language":"fr"}
                byte[] postData = payload.getBytes(StandardCharsets.UTF_8);

                HttpURLConnection connection = (HttpURLConnection)url.openConnection();
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Authorization", API_KEY);
                connection.setRequestProperty("Accept", "application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8");
                connection.setDoOutput(true);

                try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
                    wr.write(postData);
                }

                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); // Error is right here
                String inputLine;
                StringBuffer content = new StringBuffer();
                while ((inputLine = in.readLine()) != null) {
                    content.append(inputLine);
                }
                in.close();
                connection.disconnect();

                jsonObject = new JSONObject(content.toString());
            } catch (IOException | JSONException e) {
                e.printStackTrace();
            }

            return new UPSRoute(jsonObject);
        } else {
            return getRoute(start, end);
        }
    }

下面是错误:

代码语言:javascript
复制
java.io.IOException: Server returned HTTP response code: 500 for URL: https://api.openrouteservice.org/v2/directions/foot-walking/json
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1913)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1509)
    at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:245)
    at UPSRouteService.getRoute(UPSRouteService.java:63)
    at Main.main(Main.java:5)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-06 15:58:23

多亏了安德烈亚斯,它只是错过了这条线:

代码语言:javascript
复制
connection.setRequestProperty("Content-Type", "application/json");

它现在运行得很好。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56462990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档