To do:
I want to modify the HTML file's content and put my own values in it. The HTML file has JS dependencies (saved locally to reduce network usage).
Already tried:
I opened the file as a String and performed necessary String operations to put my values in place, the I pulled the String to WebView using loadData, it doesn't work as the internally referenced JS files cant be found.
I can't seem to save the file after editing due to android's regulations.
What to do?
I am looking to put this d3js gauge in the Android app.
Any suggestion on how to efficiently pull this would be highly appreciated
What worked
Read the HTML file with InputStreamReader as a string
Perform String operations to modify the contents for the desirable outcome
Now load the String in the WebView as:
webView.loadDataWithBaseURL(null, str, "text/html", "utf-8",
null);
Where str is the HTML file's modified String.
This way we can load the HTML file from assets, modify it as per our needs, use local resources in the HTML code to speed up operations.
Related
I want to read web pages (not mine and specified) and replace some words with other words in the web page loaded.
For example, let's suppose that there is a webpage that shows string "Hello, World! 07-17-2015". And I want to replace all "07" with "08". Then this page will be shown like this: "Hello, World! 08-17-2015". (There is only a string in this example, but I want to execute at any page)
I want to do this with Android. Can I make an app with this feature?
Just make a request to get the HTML content of the page (bazillions methods to do it, lots and lots of libraries, or plain HttpURLConnection).
Then take that output as String and replace what you want:
// Your network implementation
String htmlContent = response.data.replaceAll("07", "08");
Then use a WebView and load this String inside it:
webView.loadData(htmlContent, "utf-8");
I am currently trying to import selective headline from html content in my webview. I am looking at wide variety of options like json parsing or any hack will do. I was wondering if anyone has had experience with this or a brief idea on how to go about this?
Here's my example:
This is my html file content:
<div><h1><span class = "headline"> Some depressing title </span> <span class = "source" > ABCD </span> </h1> <br/> <span class = "body"> crappy body content which I do not need </span></div>
I just want to retrieve "headline" and "source" from this html in my webview, nothing else(not the body ). How do I go about defining a parameter to retrieve these? Any clues on how to do it?
Thanks!
Step 1: get the HTML source from your WebView - see this question. You basically create a JS interface that extracts your HTML source to a Java String.
Step 2: Use an HTML Parser (for example JSOUP) to parse the JAVA String into a format that you can handle easily.
Step 3: Use the parser to extract your relevant information. Here, you could use getElementsByTag('span') to get all your spans, then filter by class; or you could directly use getElementsByClass('healine') and getElementsByClass('source').
In general, you can retreive the HTML source and parse the DOM in all cases.
Edit: if you don't want to use a parser, you can extract your information by using searches on the HTML source string (finding the correct classes, then finding the indexes of '<' and '>' caracters to parse the information. This way is harder, less efficient, and less flexible, but it can be done.
I have a HTML file and when a user clicks save, a date needs to be added to this html file.
I'm using FileWriter writer = new FileWriter(myFile, true); so it is appended to the end but I have </body></html> so the date is appended to the end of this which I don't want. The date should be appended before those tags. Is there a way to append to a specific place or a more efficient way to solve this problem?
You consider redesigning your code in a way that leaved the layout (HTML) appart from the data (the dates).
You could have a HTML containing the fixed string {{dates_here}} and store the dates in a database or separate file - and then combine them when needed for viewing by constructing an insertable piece of HTML and inserting it with a simple string replace.
With JavaScript, this is not possible, however, I have a solution. Just leave out the <body> and <html> tags, and simply append the tags after appending what's needed. Ever so long ago, it's possible to actually omit those two tags. Try it!
Well, you can keep a DOM in memory of HTML document.
Use JSoup library to make one from a file or String. It will also help you to alter structure in an object oriented way and save it back easily.
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 am extracting strings from KML file, if the string contains special character like !, #, #, ', " etc. its using codes like '
I am not able to extract entire string if it is like above, by calling getNodeValue(). It is terminating the string at special character.
<name>Continue onto Royal's Market</name>
If i extract the string i am getting only ""Continue onto Royal". I want entire string as
Continue onto Royal's Market.
How to achieve this ?? If anybody familiar with this please reply to this one.
Thanks
Your problem has nothing to do with KML but is general for XML parsning:
Don't use getNodeValue(), as there is no guarantee in DOM that text isn't actually split over several nodes.
Try using getTextContent() instead.
You might also have to replace entities, as in: node.getTextContent().replaceAll("'","'");
In general I wouldnt use DOM at all for extracting data.
I'd use the XmlPullParser as its simpler to work with - and parses faster.