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));
Related
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.
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.
I am fetching few html content from my server for which I am using JSON parsing. But this converts my html content to unicode values.
For Eg: <p>Spend minimum $10 (in a single same-day receipt) at any outlet<\/p> is getting converted to,
;p>Spend minimum $10 (in a single same-day receipt) at any outlet </p>
Now if I try to set this to my WebView it displays with HTML tags itself. If I try to encode the data using TextUtils.encode it displays the text with unicode values.
Can anyone help me with this.
How should I fetch a HTML content and display it in WebView?
I am not getting your question exactly but, If you want to load HTML in web view in you can use
webView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
and if you want to convert < and > like notation you can use Jsoup Library
Guys thanks for your help. But I have solved this issue myself. I have elaborated my way of solving the issue.
What I did is,
1)convert the unicode value to Spanned like this,
Spanned ss=Html.fromHtml(;p>Spend minimum $10 (in a single same-day receipt) at any outlet </p>");
2)Now convert this Spanned to String like this,
String tempString=ss.toString();
3)And now set this to WebView which solved the problem,
webView.loadData(tempString, "text/html","UTF-8");
Actually this isn't JSON encoder converts data to HTML entities but some other layer, before it passed to JSON encoder.
JSON have nothing to do with HTML tags, usually only quotes encoded by parser (Unicode is supported by most parsers).
You probably need to change the way data is returned by server, to omit encoding of HTML tags braces to HTML entities or decoding entities backin your app.
Update:
To decode HTML entities used in HTML tags (and others too) you may use StringEscapeUtils.unescapeHTML()
To show the HTML page inside the Webview why you require the JSON. create web view inside the XML and write below code Inside the Activity you can see the HTML page.
webView = (WebView)findViewById(R.id.webView);
FrameLayout mContentView = (FrameLayout) getWindow().
getDecorView().findViewById(android.R.id.content);
final View zoom = this.webView.getZoomControls();
mContentView.addView(zoom, ZOOM_PARAMS);
zoom.setVisibility(View.GONE);
webView.loadUrl("http://www.google.co.in/");
i want to parse xml file having HTML tags and special characters,this is my xml file
while working with SAX parser HTML content in not going to parse and text skipped from Special characters on words
can any one suggest me how to do it or Any links for the sample code or explain how to do it
I have a Periodic table of elements app for android that stores most of it's data in string arrays. I am now trying to use an sqlite database instead of the arrays but am having a small problem. If I type 'android:text="¹"' directly into a TextView it will display a superscript 1(like this-> ¹), but if I store '¹' as text in a sqlite database and then use a cursor to populate that same TextView, instead of the superscript 1 being displayed I just see "¹" exactly as I typed it. How can I get the TextView to display special characters when being populated by a sqlite database? I have been struggling for a while and am stumped so any suggestions would be appreciated. Thanks!
Use the Java Unicode string notation for each special character when inserting them into your database.
For '¹', that would be: \u00b9.
Alternatively, to parse HTML tags and character entities like ¹ in a TextView, then you can probably wrap the String in a call to Html.fromHtml() before calling setText().