I'm getting a JSON response string similar to this:
<strong>B.<\/strong> Because there is no indication of Miss Manette’s feelings
The string text that I'm receiving is full of tags like <strong>, <em> and ’
“
” etc. How can I parse it to a plain String with same features?
The only way I could think of is replacing such characters and using Html.fromHtml() method. Is there a built-in parser available? How could I parse such HTML text?
Use Html.fromHtml only. It'll parse most of the tags supplied and give you the formatted output. The point to note here is that not all of the HTML tags are supported by this method. Checkout this link for more information about what tags are supported. Also check this, though it's a bit old.
If you know what text you'll be parsing, and you have tags that aren't parsed by fromHtml, your best bet would be to replace them with empty string and then use this method.
Related
I read html code from my webpage to display in my app.
How can i convert these characters into String characters in Android?
– = -
… = ...
I know those two. But I would prefer a dynamic solution if possible.
Html.fromHtml() is used primarily for converting HTML tags into spans, returning a Spanned object that retains that formatting when applied to a TextView.
However, Html.fromHtml() can also handle converting some HTML-style entities into characters.
After parsing json data and displaying it in a textview, I noticed that it has some html characters like &lsquo, &mdash, ", ', &rsquo,  , &ndash, and many others. How to deal with that?
These are html entities. To transform them to readable text use Html.fromHtml(). Like this:
textView.setText(Html.fromHtml(jsonString));
The response of a web service is the following :
<span style="color:#DDFFEE;">You have to pay you bil before <b>12 june 2012</b> for your informations.</span></br></br> You have already a large time.
I want to display this in a TextView.
I can't just simply put the response below in a String and applicate
myTextView.setText(Html.fromHtml(response));
It doesn't work because it can't be a String.
How can I do this dynamically please ?
You just need to put the String you get as a response, inside the fromHtml call, if it's not a String, maybe you just need to append .toString() to it, to get a String..
textView.setText(Html.fromHtml("<h2>This is HTML</h2>"));
You need to use Html.fromHtml() to use HTML in your XML Strings. Simply referencing a String with HTML in your layout XML will not work.
myTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
here : Similar question
I am getting some strings from json. My string contains special characters like "æ" from Næstved an many more like "ø" from køkken. But When I set Text these strings to ant textview, I get my strings printed in unusual way.
Example: For køkken I get kø ;kken.
I think I need to encode or decode my string somewhere but where I don,t know.
Please help.
Thanks in advance
The displayed version of your string represents an HTML encoded entity. You might want to verify that it is not coming in this way in your JSON data, but in any case, to decode it you can use the StringEscapeUtils.unescapeHtml4 method from Apache Commons Lang:
final String escaped = "køkken";
System.out.println(StringEscapeUtils.unescapeHtml4(escaped));
Output:
køkken
Did you check out the Latin Coding for your characters? I know the Ash character can be coded with æ and will show up æ in the browser.
Here is the a list of codes
Hope this helps!
I am trying to use Regular Expressions to decode some HTML I retrieve from a webpage.
I want to transform some <iframe> tags into links.
The code I'm using should be working fine according to me and some testprograms, however when I run it on my android device it does not find any matches (where as in the test programs it does).
The regular expression I am using is as follows (keep in mind I'm coding in Java, so I need to escape the escape character as well):
String regularExpression = "<iframe.+?src=\\\\?(\\S+).+?(><\\\\?/iframe>|\\\\?/>)";
String replacement = "<a href=$1>Youtube</a>";
input.replaceAll(regularExpression, replacement);
From what I can gather from this it should replace all <iframe> tags that have a src attribute to hyperlinks with that source. However when I feed the following input to it, it does nothing with it:
<iframe src=\"http:\/\/www.youtube.com\/embed\/s6b33PTbGxk\" frameborder=\"0\" width=\"500\" height=\"284\"><\/iframe>
The response is simply the exact same text, only with the escape-characters removed:
<iframe src="http://www.youtube.com/embed/s6b33PTbGxk" frameborder="0" width="500" height="284"></iframe>
Can someone help me and explain what I'm doing wrong? I only started learning Regular Expressions yesterday, but I just can't for the life of me figure out why this doesn't work.
The method String.replaceAll doesn't modify the string. It can't because strings are immutable. Instead it returns a new string with the result. You need to assign this result to something:
String result = input.replaceAll(regularExpression, replacement);
Also, don't use regular expressions to parse HTML.
String resultString = subjectString.replaceAll("(?=<(iframe)\\s+src\\s*=\\s*(['\"])(.*?)\\2[^>]*>).*?</\\1>", "<a href=$3>Youtube</a>");
This should work. In addition to #Mark Byers note your regex does not seem to match to your input, even with removed (double) backslashes.