簡體   English   中英

無法在Java HTTP POST請求中發送JSON

[英]Can't send JSON in a Java HTTP POST request

盡管我檢查了我要發送的內容,但出現了“服務器返回的HTTP響應代碼:500”錯誤(我什至嘗試使用在線工具發送它,但它仍然有效)。 API密鑰和JSON是正確的。 嘗試使用“ connection.getInputStream()”讀取輸入流時出現此錯誤。 這可能從哪里來? 我忘記了什么嗎? 我正在嘗試通過openrouteservice API實現此功能: https ://openrouteservice.org/dev/#/api-docs/v2/directions/ {profile} / post

    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);
        }
    }

這是錯誤:

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)

感謝Andreas,它只是錯過了這一行:

connection.setRequestProperty("Content-Type", "application/json");

現在工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM