发布于 2016-02-22 07:07:59 | 547 次阅读 | 评论: 0 | 来源: 网友投递
Android移动端操作系统
Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。
今天遇到这样一个bug:客户端POST到服务器的一段数据导致服务器端发生未知异常。服务器端确认是编码转换错误。于是截取网络数据包进行分析,发现客户端POST的json数据中包含下面一段(hex形式):
... 61 64 20 b7 20 52 69 63 ...
由于系统使用了ormlite、gson和async-http几个库,于是逐一排查。最后发现原来是向服务器发送数据时没有指定文字编码,导致async-http(实际是apache common http client)将数据以ISO-8559-1格式发送,U+00b7被编码成b7,然后服务器试图使用UTF-8解码时发生错误。
出错的代码片段如下:
Gson gson = new Gson();
String json = gson.toJson(data);
StringEntity entity = new StringEntity(json);
httpClient.post(context, url, entity, "application/json", new TextHttpResponseHandler() ... );
Gson gson = new Gson();
String json = gson.toJson(data);
StringEntity entity = new StringEntity(json, "utf-8");
httpClient.post(context, url, entity, "application/json;charset=utf-8", new TextHttpResponseHandler() ... );