开发者

Android charset problem downloading utf-8 webpage

I have a problem downloading and parsing a UTF-8 webpage... I use the next function to get the web's HTML:

static String getString(String url, ProgressDialog loading) {
    String s = "", html = "";
    HttpURLConnection conn = null;
    try {
        conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
        conn.setConnectTimeout(5000);
        conn.setReadTimeout(5000);
        conn.connect();
        DataInputStream dis = new DataInputStream(conn.getInputStream());
        loading.setTitle("Descargando...");
        loading.setMax( 32000 );
        while ((s = dis.readLine()) != null) {
            html += s;
            loading.setProgress(html.length());
        }
    } catch (Exception e) {
        Log.e("CC", "Error al descargar: " + e.getMessage());

    } finally {
        if (conn != null)
            conn.disconnect();
    }
    return html;
}

And the web page has:

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

But the Spanish's elements like: ¡ ¿ á é í ó ú apears wrong in the app. I tried to use readUTF() but I have length pr开发者_Go百科oblems...

Some ideas? Thank you!


You need to use a Reader where you specify the charset used to read the input stream. In this particular case you need an InputStreamReader.

Reader reader = null;
StringBuilder builder = new StringBuilder();

try {
    // ...
    reader = new InputStreamReader(connection.getInputStream(), "UTF-8");
    char[] buffer = new char[8192];

    for (int length = 0; (length = reader.read(buffer)) > 0;) {
        builder.append(buffer, 0, length);
        loading.setProgress(length);
    }
} finally {
    if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
}

String html = builder.toString();
// ...

Unrelated to the concrete problem, did you consider using a HTML parser like Jsoup? It'll take this nasty details into account. It's then as simple as

String html = Jsoup.connect(url).get().html();
// ...

It however doesn't really allow for attaching a progress monitor.


I'm pretty sure you don't want to use a DataInputStream.

This answer might be helpful though: Read/convert an InputStream to a String

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜