【发布时间】:2014-06-15 14:27:12
【问题描述】:
StackOverflowers!
我从网页加载了一些文本。一切正常,并显示在 TextViews 中。但是,当网页上的博客有一些带有特殊字符的单词时,例如:é 或其他东西。我的 Textview 显示如下字符:Á©..
谁能告诉我需要导入、调用或类似的东西来显示一切整洁?
谢谢,
更新 1 + 2:
TextView intro_text = (TextView) item_view.findViewById(R.id.item_intro);
intro_text.setText(Html.fromHtml(current_post.get_intro()));
current_post.get_intro() 是加载文本的地址。 :-) 因为我使用了很多行的列表视图..
编辑:
public class connectTask extends AsyncTask<String, String, String> {
@SuppressWarnings({ "deprecation" })
@Override
protected String doInBackground(String... message) {
Log.v("Correct Load", "Starting ");
URL u;
InputStream is = null;
DataInputStream dis;
String s;
try {
Log.v("Connecting...");
u = new URL("http://.......");
is = u.openStream();
dis = new DataInputStream(new BufferedInputStream(is));
Log.v("Connected");
try {
while ((s = dis.readLine()) != null) {
if (s.contains("post_wrapper")) {
for (int i = 0; i < j; i++) {
while ((s = dis.readLine()) != null) {
if (s.contains("post_intro")) {
break;
}
}
if (s != null) {
s = dis.readLine();
Log.v("Intro", s); intro[i] = s.substring(s.indexOf("<p>") + 3, s.indexOf("</p>"));
Log.e("Intro", "Found intro:" + intro[i]);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (MalformedURLException mue) {
System.out.println("Ouch - a MalformedURLException happened.");
mue.printStackTrace();
System.exit(1);
} catch (IOException ioe) {
System.out.println("Oops- an IOException happened.");
ioe.printStackTrace();
System.exit(1);
} finally {
try {
if (is != null)
is.close();
} catch (IOException {
}
}
return null;
}
这是读取/接收的部分。
【问题讨论】:
-
这是一个编码问题。您的 TextViews 不使用与您的 WebService 相同的编码(希望它使用 UTF-8)。
-
我最近也遇到了这个问题。事实证明,HTTP 请求和响应的默认编码是 ISO-8859-1,它将这些特殊字符编码为单个字符,但 UTF-8 将它们编码为双字符。所以当 UTF-8 被解码为 ISO-8859-1 时,你最终会得到多个不同的字符(通常带有 Á)
-
@Bob Malooga 是的,在我的布局 xml 之上,我有那一行..
-
@Dan Temple 你到底是什么意思?我需要做什么/尝试什么?
-
好吧,我不太确定它如何应用于 Android,但您可以随时尝试:
text.setTest(Html.fromHtml( URLDecoder.decode( stringVariable, "UTF-8"); ).toString());
标签: java android html utf-8 textview