在 android 的 HttpURLConnection 中无法从 POST JSON 获得响应

Unable to get a response from POST JSON in HttpURLConnection in android

我正在尝试 post 一个看起来像

的 JSON
{
    "latLong":"50.1109,8.6821 - latLong",
    "currencyCode":"EUR",
    "locale":"en-GB",
    "budget":""
} 

当我使用 Postman 执行此操作时,我得到了我需要的响应。但在 android studio 中它无法编译,我得到一个空字符串。 我的函数看起来像这样。

protected Void doInBackground(Void... params) {

        URL url = null;
        StringBuffer sb = new StringBuffer();

        try {
            url = new URL("http://192.168.2.101:12345/services/test");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        HttpURLConnection con = null;
        try {   
            con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("POST");
            con.setDoOutput(true);
            con.setRequestProperty("Content-Type","application/json");
            DataOutputStream printout = new DataOutputStream(con.getOutputStream());

            String Json_String = "{\n" +
                    "    \"latLong\":\"50.1109,8.6821 - latLong\",\n" +
                    "    \"currencyCode\":\"EUR\",\n" +
                    "    \"locale\":\"en-GB\",\n" +
                    "    \"budget\":\"\"\n" +
                    "}";

            InputStream in = new BufferedInputStream(con.getInputStream());
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String inputLine = "";
            while ((inputLine = br.readLine()) != null) {
                sb.append(inputLine);
            }
            result = sb.toString();

            printout.flush ();
            printout.close ();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            responseMsg = con.getResponseMessage();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            response = con.getResponseCode();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

使用 OutputStreamWriter 而不是 DataOutputStream,con.setDoOutput(true) 应该在发布之前。

基本上:

   con = (HttpURLConnection) url.openConnection();
   con.setDoOutput(true);
   con.setRequestMethod("POST");

   OutputStreamWriter writer = new OutputStreamWriter(
            con.getOutputStream());
   String Json_String = "{\n" +
            "    \"latLong\":\"50.1109,8.6821 - latLong\",\n" +
            "    \"currencyCode\":\"EUR\",\n" +
            "    \"locale\":\"en-GB\",\n" +
            "    \"budget\":\"\"\n" +
            "}";

   writer.write(Json_String);
   writer.close();

   if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
        // OK
        InputStream in = new BufferedInputStream(con.getInputStream());
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String inputLine = "";
        while ((inputLine = br.readLine()) != null) {
            sb.append(inputLine);
        }
        string result = result = sb.toString();