美文网首页码农的世界Java
android HttpPost传JSON数据中文乱码的解决方法

android HttpPost传JSON数据中文乱码的解决方法

作者: 一觉睡到丶小时候 | 来源:发表于2019-09-17 10:48 被阅读0次

在项目中使用HttpPost方式向服务器提交JSON数据时,服务器上接收的json数据竟然显示乱码,查找了不少方法,最终一个解决了我的问题,我的部分代码如下:

HttpPost hp = new HttpPost(address);
                JSONObject jo = new JSONObject();
                try {
                    jo.put("stationname", stationName);
                    jo.put("personname", personName);
                    jo.put("fileTime", fileTime);
                    jo.put("filetype", fileType);
                    System.out.println(jo);
                    hp.setEntity(new StringEntity(jo.toString(), HTTP.UTF_8));
                    hc.execute(hp);
                }

原代码中 hp.setEntity(jo.toString()); 不能正确的传输中文。改成这样:

hp.setEntity(new StringEntity(jo.toString(), HTTP.UTF_8));

要使用utf-8将请求参数进行编码后传输,在服务器端就能正确的读出json传输的中文字符了。

相关文章

网友评论

    本文标题:android HttpPost传JSON数据中文乱码的解决方法

    本文链接:https://www.haomeiwen.com/subject/slvquctx.html