BufferedReader UTF-8 BOM

BufferedReader UTF-8 BOM

我在 Java 中有一个方法,我尝试通过 POST json 请求获取一个整数(用户 ID),这是方法:

public static Integer getUserIdOwnerOfMsg(Message msg) {
        Integer idUser = null;
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("textMsg", msg.getTextMsg());
            jsonObject.put("hourMsg", msg.getHourMsg());

            List list = new LinkedList();
            list.addAll(Arrays.asList(jsonObject));
            String jsonString = list.toString();

            String urlStr = SERVER_PATH + "getUserIdOwnerOfMsgJSON.php";
            URL url = new URL(urlStr);

            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("POST");
            con.setRequestProperty("User-Agent", "your user agent");
            con.setRequestProperty("Accept-Language", "sp,SP;q=0.5");
            //con.setRequestProperty("Content-Type", "text/html; charset=UTF-8");

            String urlParameters = "json=" + jsonString;

            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();

            InputStream instream;

            int status = con.getResponseCode();

            if (status != HttpURLConnection.HTTP_OK)
                instream = con.getErrorStream();
            else
                instream = con.getInputStream();

            BufferedReader in = new BufferedReader(new InputStreamReader(instream, "UTF-8")); // ISO-8859-1
            in.mark(1);
            if(in.read() != 0xFEFF)
                in.reset();
            String inputLine;
            StringBuilder response = new StringBuilder();

            while((inputLine = in.readLine()) != null)
                response.append(inputLine);

            in.close();

            JSONObject object = new JSONObject(response.toString());
            Boolean correct = object.getBoolean("correct");
            if (correct) {
                idUser = object.getInt("id");

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return  idUser;
    }

问题是响应包含 UTF-8 BOM 字符,在邮递员上结果没问题,我可以看到 ID:({"id":"7","correct":true})

但是在 android studio 上调试我得到 ""null 的值 idUser,我不知道为什么,我已经尝试了很多方法解决这个问题没有成功。

同样在 php 服务器端,我执行了 echo mb_detect_encoding($idUsuario); 并且我得到了 ASCII 编码以防万一可以帮助找到解决方案。

任何帮助将不胜感激,谢谢!

所以我终于找到了解决方案,我在这里 post 以防有人遇到与我相同的问题,问题出在 JSON 的参数上,因为它们包括 [=13] =] 像 这样的字符,所以不要像这样读取参数:

wr.writeBytes (urlParameters);

我必须这样做:

byte[] buf = urlParameters.getBytes("UTF-8");
wr.write(buf, 0, buf.length);

我post还有完整的功能以备不时之需:

public static Integer getUserIdOwnerOfMsg(Message msg) {
    Integer idUser = null;
    try {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("textMsg", msg.getTextMsg());
        jsonObject.put("hourMsg", msg.getHourMsg());

        List list = new LinkedList();
        list.addAll(Arrays.asList(jsonObject));
        String jsonString = list.toString();

        String urlStr = SERVER_PATH + "getUserIdOwnerOfMsgJSON.php";
        URL url = new URL(urlStr);

        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", "your user agent");
        con.setRequestProperty("Accept-Language", "sp,SP;q=0.5");

        String urlParameters = "json=" + jsonString;

        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream (con.getOutputStream());
        byte[] buf = urlParameters.getBytes("UTF-8");
        wr.write(buf, 0, buf.length);
        wr.flush();
        wr.close();

        InputStream instream;

        int status = con.getResponseCode();

        if (status != HttpURLConnection.HTTP_OK)
            instream = con.getErrorStream();
        else
            instream = con.getInputStream();

        BufferedReader in = new BufferedReader(new InputStreamReader(instream, Charset.forName("UTF-8")),8192);
        String inputLine;
        StringBuilder response = new StringBuilder();

        while((inputLine = in.readLine()) != null)
            response.append(inputLine);

        JSONObject object = new JSONObject(response.toString());
        Boolean correct = object.getBoolean("correct");
        if (correct) {
            try {
                String idUserText = object.getString("id");
                idUser = Integer.valueOf(idUserText);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        in.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return  idUser;
}

就这些,祝编码愉快!