Wanglin Luo avatar

Alamofire response 中文乱码

luowanglin

Published: 07 Mar 2019 › Updated: 07 Mar 2019Alamofire response 中文乱码

Alamofire response 中文乱码

Alamofire response 中文乱码
痛点:最近使用Alamofire进行数据请求,发现使用HandyJSON解析String对象时,中文属性值全部乱码;

产生的原因:不管是request乱码还是response乱码,其实都是由于客户端(浏览器)跟服务器端采用的编码格式不一致造成的。以request乱码为例:浏览器向服务器发送请求,因为浏览器与服务器之间的通信实质上是socket流,所以要先将请求参数(字符)转换成字节,也就是编码过程,服务器接收到请求参数后进行解码(字节转字符),然后封装到request对象中。如果客户端的编码与服务器端的解码不统一,就会导致通过request获取到的请求参数的值是乱码。

解决方案:结合项目分析,因为该接口返回的是JSON,所以我改用了Alamofire中的responseJSON链方法,替换原来的responseString()。同时项目数据对象模型用的是HandyJSON,所以同时又引用了SwiftyJSON进行辅助解析成String对象供HandyJSON使用,具体解决方案如下图:(T:为HandyJSON泛型)

Alamofire response 中文乱码
原文始发于:Alamofire response 中文乱码


Posted from my blog with SteemPress : http://cnstm.org/blog/2018/04/23/168bf192f8/

Leave Alamofire response 中文乱码 to:

Written by

Mobile Application Developer

Read more #steempress posts


Best Posts From Wanglin Luo

We have not curated any of luowanglin's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Wanglin Luo