将utf-8网址读取为字符串java [英] Read utf-8 url to string java

查看:131
本文介绍了将utf-8网址读取为字符串java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天.刚从Objective-c切换到Java,并尝试将URL内容正常读取为字符串.阅读大量帖子,仍然会产生垃圾.

Good day. Have just switched from objective-c to java and trying to read url contents normally to string. Read tons of posts and still it gives garbage.

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();
        }
    }
}

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

What's wrong? I get something like this

?? y ??'?????)j1 ???-?q?E?| V ??,??< 9 ?? d?Bw(?? n?v?)i?x ????? Z ???? q?MM3〜?????? G ???????? l?U3"Y? ] ???? zxxDx ???? t ^ ??? 5 ??? j?‌?k ?? u?q?j6?^ t ??????? W ???? ??????〜????????? o6/?|?8 ?? {??? O ???? 0?M> Z {srs ?? K ??? XV ?? 4Z‌ ??'????? n/?? ^ ?? 4 ???? w + ????? e ??????? [?{/??,?? WO ???? ???????.?.?x ??????? ^?rax ??]?xb ?? ‌& ?? 8; ?????}} ??? h ??? ?H5 ???? v?e?0 ??????-????? g?vN

??y??'??)j1???-?q?E?|V??,??< 9??d?Bw(?э?n?v?)i?x?????Z????q?MM3~??????G??љ??l?U3"Y?]????zxxDx????t^???5???j?‌​?k??u?q?j6?^t???????W??????????~?????????o6/?|?8??{???O????0?M>Z{srs??K???XV??4Z‌​??'??n/??^??4????w+?????e???????[?{/??,??WO???????????.?.?x???????^?rax??]?xb??‌​& ??8;?????}???h????H5????v?e?0?????-?????g?vN

推荐答案

以下是使用HttpClient的方法:

Here is a method using 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.

I edited the response to ensure it uses utf-8.

这篇关于将utf-8网址读取为字符串java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆