1. 程式人生 > >HttpURLConnection 接收網絡數據出現亂碼問題

HttpURLConnection 接收網絡數據出現亂碼問題

!= new zip 網絡數 代碼實現 cnblogs 部分 end 。。

由於接收的數據經過gZip處理過,所以在接受的時候也要處理,並且加上編碼格式(沒有會出現部分數據亂碼):

具體代碼實現如下:

URL ul = new URL(url);
HttpURLConnection conn = (HttpURLConnection) ul.openConnection();
conn.connect();
InputStream in = conn.getInputStream();

StringBuffer sb = new StringBuffer();
String readLine = new String();
GZIPInputStream gZipS=new GZIPInputStream(in);
InputStreamReader res = new InputStreamReader(gZipS,"UTF-8");
BufferedReader responseReader = new BufferedReader(res);
while ((readLine = responseReader.readLine()) != null) {
	sb.append(readLine);
}
responseReader.close();

測試問題解決。。

HttpURLConnection 接收網絡數據出現亂碼問題