StackOverflowers!
I load some text from a webpage. Everything works fine, and shows up in the TextViews. But, when the blogs on the webpage have some words with special characters like an: é or something. My Textview show characters like these: Á©..
Can anybody tell what I need to import, call or something like that to show everything tidy?
Thanks,
UPDATE 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() is the adres where the loaded text is. :-) Because I use a listview with many rows..
EDIT:
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;
}
This is the read/received part.
tricky but worked for me: use Html.fromHtml()
twice , I mean:
text.setText(Html.fromHtml(Html.fromHtml(your_html_text).toString()));
EDIT .
Your problem is not in this TextView
because you are getting broken encoding even in your logcat, so you should know when exactly , the encoding is broken ; this get_intro()
is returning a bad string, so you should show us , what is this get_intro()
doing ? how is it taking the string ? from what ? you should share the code of this get_intro()
else nobody can help you...
Replace
DataInputStream dis = new DataInputStream(new BufferedInputStream(is));
With
BufferedReader dis = new BufferedReader(new InputStreamReader(is, "UTF-8"));
Because DataInputStream.readLine()
is deprecated and is discouraged for the following reason:
This method cannot be trusted to convert bytes to characters correctly.
BufferedReader
also has a readLine
method, so the rest of your code should be pretty much unchanged.
Also, whenever you use @SuppressWarnings({ "deprecation" })
I strongly suggest that you be extra careful and make sure you can use the deprecated method despite the warning.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.