CC3200 HTTP Cli通过POST的方式把json数据提交给服务器出现的问题

来源:互联网 发布:小米3如何使用4g网络 编辑:程序博客网 时间:2024/06/05 13:04

 

                                                                                                                                                                                        阿汤哥

序:

能力有限,难免有错,有问题请联系我,

QQ1519256298       hytga@163.com

Pdf下载http://pan.baidu.com/s/1hqiWB56

 

请采用邮件联系的方式:谢谢。

CC3200使用http cli,用json的格式发送数据给服务器,服务器使用fastjson的容易出错的错误,及解决办法。

错误一:编码导致的错误

今天用cc3200发送中文给servlet,结果出错了。

错误代码:java.nio.charset.MalformedInputException: Input length = 1

image

 

按照习惯,我都是servlet都是设置成utf-8,对编码这一方面不太了解,因此仅仅给出解决方案

,设置成request.setCharacterEncoding("GB2312");

image

 

 

 

 

错误二 :json数据后面有莫名其妙的空格,我怀疑这是官方的http cli自己的问题

错误代码:not close json text, token : error

这个破问题花了好久。

代码如下

request.setCharacterEncoding("gb2312");response.setContentType("text/html; charset=utf-8");PrintWriter out = response.getWriter();BufferedReader reader = request.getReader();String line = null;String text = null ;//先读一行数据,为什么需要先这样做?因为text的初始值是null,//执行text += line;后会导致text=“null”+line,所以需要先读一行//这里可能会有bug存在,请注意text = reader.readLine();while((line = reader.readLine()) != null){text += line;}System.out.println("|"+text+"|");JSONObject root=  JSON.parseObject(text);System.out.println("解析成功");String type = (String) root.get("type");System.out.println("type:"+ type);

出错信息

信息: Reloading Context with name [/SmartHardware] is completed|{"type":"DvcCreateDvc","id":"cc3200Test","name":"cc3200Test","point":[{"id":"1","name":"温度","type":"FloatWW","unit":"C"},{"id":"2","name":"开关","type":"SwitchWW","unit":"none"}]}七月 30, 2015 9:34:51 下午 org.apache.catalina.core.StandardWrapperValve invoke严重: Servlet.service() for servlet [DeviceHandle] in context with path [/SmartHardware] threw exceptioncom.alibaba.fastjson.JSONException: not close json text, token : errorat com.alibaba.fastjson.parser.DefaultJSONParser.close(DefaultJSONParser.java:1282)at com.alibaba.fastjson.JSON.parse(JSON.java:106)

上面的出错信息有我的json信息,我左看右看,貌似都是没问题的

后面才发现json的末尾数据有莫名其妙的空格,我也不知道这是不是空格

image

 

解决办法:加trim去掉首尾的空格,我真想说这fastjson的要求也太高了吧,,空格都会导致错误,,,或者这不是空格???

image

0 0
原创粉丝点击