java一个头两个大的字符编码问题

goodlucktomyself 2015-10-28 06:08:47
大概情况是这样, Android调用:
http://wthrcdn.etouch.cn/weather_mini?citykey=101280601
, 来获取相应城市的天气预报情况, 然后显示出来.
我用火狐浏览器打开, 显示乱码. 然后查看其页面信息, 发现显示: gbk编码, 改变浏览器编码成utf-8, 乱码解决. 于是乎: 我在Android代码里也转码(gbk --> utf-8), 依旧是乱码!!!
PS: 各种其它转码也试了(gbk-->unicode, gbk-->utf8等), 都是无动于衷, 顿时凌乱!!!


求解???
...全文
111 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
goodlucktomyself 2015-10-30
  • 打赏
  • 举报
回复
知道问题所在了, 该网站对相应内容做了压缩处理,其请求header中含: Content-Encoding:gzip , 所以需要解压如下:

InputStream in = response.getEntity().getContent();
public static String uncompress(InputStream in) throws IOException {
    if (in== null) {
          return "";
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPInputStream gunzip = new GZIPInputStream(in);
    byte[] buffer = new byte[256];
    int n;
    while ((n = gunzip.read(buffer)) >= 0) {
      out.write(buffer, 0, n);
    }
    out.flush();
    out.close()
    return out.toString("utf-8");
  }
goodlucktomyself 2015-10-29
  • 打赏
  • 举报
回复
引用 1 楼 xiaofanku 的回复:
天气服务的响应内容字符集是:gbk?

是啊, 而且一般网站的渲染模式都是 "标准模式", 它的是"怪异模式", 不知道跟这个有关系没?
街头小贩 2015-10-28
  • 打赏
  • 举报
回复
天气服务的响应内容字符集是:gbk?

25,980

社区成员

发帖
与我相关
我的任务
社区描述
高性能WEB开发
社区管理员
  • 高性能WEB开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧