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

时间:2017-02-19 16:54:50

标签: android http-post httpurlconnection

我正在尝试发布一个看起来像

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

1 个答案:

答案 0 :(得分:0)

使用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();