读取utf-8 url到字符串java

时间:2012-09-25 21:48:47

标签: java string url utf-8

美好的一天。刚刚从objective-c切换到java并尝试将url内容正常读取为字符串。阅读大量的帖子,但它仍然会产生垃圾。

public class TableMain {

    /**
     * @param args
     */
    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws Exception {
        URL url = null;
        URLConnection urlConn = null;

        try {
            url = new URL("http://svo.aero/timetable/today/");
        } catch (MalformedURLException err) {
            err.printStackTrace();
        }
        try {
            urlConn = url.openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader input = new BufferedReader(new InputStreamReader(
                    urlConn.getInputStream(), "UTF-8"));
            StringBuilder strB = new StringBuilder();
            String str;
            while (null != (str = input.readLine())) {
                strB.append(str).append("\r\n");
                System.out.println(str);
            }
            input.close();
        } catch (IOException err) {
            err.printStackTrace();
        }
    }
}

怎么了?我得到这样的东西

  ????

,θY ??“??)J1 ??? - q E | V 10,??< 9 ?? d?体重(?э-N-V')我?X ?????Ž???? Q'MM3〜??????ģ??љomega; L U3" Y? ] ???? zxxDx ????吨^ ??? ??? 5 J··K ??úq + J6?^ T ???????W¯¯???? ??????〜O6 ????????? / | 8 ?? {???ö???? 0 M>有 - Z {SRS进口K ??? XV ?? 4Z ??“?? N / ?? ^ ?? 4 ????瓦特+···é____ [{/ ??,?? WO ???? ?????????????????????????????????????????????????????????? ?H5 ???????????????????????????

2 个答案:

答案 0 :(得分:-1)

这是一个使用HttpClient的方法:

 public HttpResponse getResponse(String url) throws IOException {
    httpClient.getParams().setParameter("http.protocol.content-charset", "UTF-8");
    return httpClient.execute(new HttpGet(url));
}


public String getSource(String url) throws IOException {
            StringBuilder sb = new StringBuilder();
            HttpResponse response = getResponse(url);
            if (response.getEntity() == null) {
                throw new IOException("Response entity not set");
            }
            BufferedReader contentReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            String line = contentReader.readLine();

            while ( line != null ){
                sb.append(line)
                  .append(NEW_LINE);
                line = contentReader.readLine();
            }
            return sb.toString();
    }

编辑:我编辑了响应以确保它使用utf-8。

答案 1 :(得分:-1)

这是以下结果:

  1. 您正在获取UTF-8编码的数据
  2. 您没有指定,但我猜测您正在将其打印到Windows系统上的控制台
  3. 正在接收和存储数据,但是当您打印数据时,目标无法呈现俄语文本。除非最终的显示处理程序能够渲染所涉及的字符,否则您将无法将文本“打印”到stdout。