HttpURLConnection中文乱码分析和解决
2017-12-01 17:19
453 查看
HttpURLConnection中文乱码分析和解决
产生中文乱码一般都是编码格式不匹配导致的,例如后台使用UTF-8编码格式,而移动端在接收数据时采用Iso 或者 GBK等格式,而往往我们所使用的网络编程工具在我们步明确指定编码格式的情况下给我们指定的默认格式并非UTF-8.比如下面这段代码就会导致中文乱码
URL url = new URL(RequestURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); InputStream input = conn.getInputStream(); StringBuffer sb1 = new StringBuffer(); int ss; while ((ss = input.read()) != -1) { sb1.append((char) ss); result = sb1.toString();
在这样的操作下,有可能会出现接收到的中文是乱码的,比如后台返回的格式是”UTF-8”,接收到的中文就会是乱码
解决方法进行转码
InputStream input = conn.getInputStream(); InputStreamReader reader=new InputStreamReader(input,"UTF-8"); StringBuffer sb1 = new StringBuffer(); int ss; while ((ss = reader.read()) != -1) { sb1.append((char) ss); result = sb1.toString();
**注:**Volley在后台步明确指定编码格式的情况下也会默认采用ISO-8859-1格式
相关文章推荐
- 解决HttpURLConnection请求时传中文参数乱码问题
- HttpURLConnection的URL包含中文字符时乱码的解决办法
- 解决httpurlconnection获取网页数据部分中文乱码问题
- Java HttpURLConnection模拟请求Rest接口解决中文乱码问题
- HttpURLConnection以post传数据时中文乱码的一个解决方法
- HttpURLConnection中文乱码解决
- Java HttpURLConnection模拟请求Rest接口解决中文乱码问题
- 解决HttpURLConnection中文乱码问题
- Java中HttpURLConnection返回头Location域中文乱码问题
- HttpURLConnection请求后,返回数据出现乱码解决办法!
- httpurlconnection post 和GET 分别传递2个参数给后台,返回JSON 解决乱码问题
- HttpURLConnection获取网页内容,解决乱码的通用方法
- 解决HttpUrlConnection乱码问题
- 关于Android使用HttpURLConnection传输含有中文JSON数据的报错、乱码问题
- 转载---HttpUrlConnection发送post请求汉字出现乱码的一个解决方法及其原因
- (一)HttpURLConnection之Get请求(解决乱码)
- 解决HttpUrlConnection乱码问题
- 用HttpURLConnection请求网页的时候会出现中文乱码
- android post HttpURLConnection乱码解决
- android asp.net HttpURLConnection通信 中文乱码