【问题标题】:InputStream converting punctuation to UnicodeInputStream 将标点符号转换为 Unicode
【发布时间】:2017-07-12 09:49:24
【问题描述】:

我的应用程序连接到互联网并清理页面以获取 html 以获取图像和文本等内容。但是我注意到一些标点符号实际上被转换为它的 unicode 十进制代码,无论如何要阻止这个?

public class DownloadPage extends AsyncTask<String, Void, String> {

    public interface PageResponse {
        void processFinish(String output);
    }

    private PageResponse delegate = null;

    public DownloadPage(PageResponse delegate){
        this.delegate = delegate;
    }

    @Override
    protected String doInBackground(String... urls) {
        URLConnection connection;
        try {
            URL url = new URL(urls[0]);

            connection = url.openConnection();

            String html;
            InputStream inputStream = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder str = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                str.append(line);
            }
            inputStream.close();
            html = str.toString();

            return html;

        } catch (MalformedURLException e) {
            e.printStackTrace();
            return "Failed";
        } catch (IOException e) {
            e.printStackTrace();
            return "Failed";
        }
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        delegate.processFinish(s);
    }
}

这是我从中获取信息的页面,https://www.looemusic.co.uk/news/

This is what comes up with this code.

【问题讨论】:

    标签: java android html unicode android-asynctask


    【解决方案1】:

    如果您确定问题出在您的 InputStream,而不是 HTML 呈现本身,那么您可以设置 InputStreamReader 的字符集:

    new InputStreamReader(inputStream, Charset.UTF-8);
    

    此字符集来自 Java.nio.charset。

    如果失败,您可以检查问题是否与客户端的编码有关。将此标签放在您的 HTML 文件中:

    对于 HTML 5:

    <meta charset="UTF-8">
    

    对于 HTML 4:

    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    

    如果您想使用其他字符集而不是 UTF-8,那么只需更改代码中的名称!

    【讨论】:

    • 它不喜欢 Charset.UTF-8 。这不是一个选择
    猜你喜欢
    • 1970-01-01
    • 2018-11-16
    • 2015-03-07
    • 2011-12-19
    • 2015-10-18
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多