Using WebView.Load() to read dynamic HTML with references to JS libraries? - java

I'm trying to write a class that will return a string of HTML with Javascript for use in a WebView. Returning HTML and Javascript works well, but I'm having issues loading libraries with the returned HTML. As an example, this works well:
public static String helloWorld() {
String html;
html = "<!DOCTYPE html>" +
"<html>" +
"<head>" +
"<title>" +
"</title>" +
"</head>" +
"<body>" +
"<script>" +
"document.write(\"Hello, World\");" +
"</script>" +
"</body>" +
"</html>";
return html;
}
However, this does not:
public static String helloWorld() {
String html;
html = "<!DOCTYPE html>" +
"<html>" +
"<head>" +
"<title>" +
"</title>" +
"<script src=\"../assets/src/external_script.js\"></script>" +
"</head>" +
"<body>" +
"<script>" +
"external_script_function();" +
"</script>" +
"</body>" +
"</html>";
return html;
}
I'm guessing that the filepath for the external_script.js import is incorrect? Using WebView.LoadUrl(myLocalHtmlFile); works when using the other JS file. How would I go about making this work properly? Or alternatively, is there a better way to achieve similar results?

You need to feed the assets folder to the WebView as the base URL. Like this:
MyWebView.loadDataWithBaseURL("file:///android_asset/", helloWorld());
And then the <script> tag would go like this:
<script src="src/external_script.js"></script>
Assuming the library resides under assets/src. And yes, you need to enable JavaScript like triad is suggesting.

Since you write assets i suppose you are trying to load a file you put in Eclipse or what ever IDE you use that is bundled with your app. This gets stored inside your APK and AFAIK it does not get "unzipped" to the working-directory of your app.
This means that eighter you will have to manually place it on the SDcard or internal memory or load it internally and dynamically inject it into the final HTML before you send it to your WebView.
Personally i would go for the second method because it does make less of a mess.

Related

Extract html part from string (include plain text and html part) using java

I have an inputstream from email which can be converted to string like this:
String content = "Hello world!\n"+
"Thank you!\n"+
"\n"+
"<html>\n" +
"<head>\n" +
"\t<meta id=\"leadId\" name=\"leadId\" content=\"6778130\"/>\n" +
"\t<title>testing</title>\n" +
"</head>\n" +
"<body>\n" +
"\t<span>testing - 20200727</span>\n" +
"</body>\n" +
"</html>"+
"\n" +
"Have a good day!";
I wanna extract HTML part from this string, the result I expect is like:
<html>
<head>
<meta id="leadId" name="leadId" content="6778130"/>
<titletesting</title>
</head>
<body>
<span>testing - 20200727</span>
</body>
</html>
I tried Jsoup before, but it didn't work for me.
Does anyone know other solutions to it?
Can I use javax.mail for it (the inputstream itself)? If so, how can I do that? Could you provide an example?
My approach - use a regex to extract the bit of text you're interested in.
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
Pattern p = Pattern.compile("<html>.*</html>");
Matcher m = p.matcher(inputString);
String html = m.group();
You could then use JSoup.parse(html); to parse the html and navigate the elements. (Or us HtmlUnit if you want to navigate the document using XPaths).

XPath parsing failing with dom4j for text function

My input xml is
String xml= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<disks-array>\n" +
"<array-item>\n" +
" <value>\n" +
"<scsi>\n" +
"<bus>0</bus>\n" +
"<unit>0</unit>\n" +
"</scsi>\n" +
"<backing>\n" +
"<vmdk_file>[909_TCUP_02] u999orcat017t/u999orcat017t.vmdk</vmdk_file>\n" +
"<type>VMDK_FILE</type>\n" +
"</backing>\n" +
"<label>Hard disk 1</label>\n" +
"<type>SCSI</type>\n" +
"<capacity>107374182400</capacity>\n" +
"</value>\n" +
"<key>2000</key>\n" +
"</array-item>\n" +
"</disks-array>"
and the XPath filter is
"//array-item[contains(./value/backing/vmdk_file/text(),'u999orcat017t/u999orcat017t.vmdk')]"
Here is my parsing and filtering code
Document doc = DocumentHelper.parseText(xml);
XPath xp = DocumentHelper.createXPath(xpathQuery);
// evaluate the xpath
Object xpResult = xp.evaluate(doc);
Ideally it should return me the array items /value/vmdk_file text contains the given text. However it gives me empty string.
I am using dom4j 1.61 and jaxen 1.1.1 version library.
What is going wrong ?
Finally after debugging for many hours figured out the root cause for incorrect parsing of xml. The text value is broken into multiple nodes instead of single node. See the highlighted picture
Turns out this is a bug in dom4j library which is still open
https://github.com/dom4j/dom4j/issues/21
The fix is to call document.normalize() to settle text nodes.

Java Mail Embed URL

When I'm adding an HTML URL into email body, it is not redirecting to the preferred location. This is the snippet, please tell me what am I doing wrong.
#location variable contains the URL
StringBuffer body = new StringBuffer("<html><body>Hi, <br/><br/>");
body.append("<p>"+cmts+"</p>");
#both the ways are not working, how to construct proper URL
body.append("<br/><br/>" + location + "<br/>");
body.append("<br/><br/>" +location + "<br/>");
#this is working as link only in OUTLOOK, but in other mail client it shows as plain text
body.append("<br/><br/>"+location);
URL:
http://host:port/weebApp/report/viewer.html#%2Fpublic%2FSamples%2FDashboards%2_FSample_report
It looks like a problem with the quotation marks. Try:
body.append("<br/><br/>" + location + "<br/>");
There could be many ways to add href in javamail for example:
1) InternetHeaders headers = new InternetHeaders();
headers.addHeader("Content-type", "text/html; charset=UTF-8");
String aHref = "some text\n" + text +
"\n<a href='http://google.com'>google.com</a>";
2) String aHref = "some text\n" + text +
"\n<a href='http://google.com'>google.com</a>";
messageBodyPart.setText(aHref,"UTF-8","html");
UPDATE:
Make sure the content-type is set to html or text/html because text/plain will display it as only text

In Java, how to delete everything after .com or .net

I am trying to get just the domain name (http://www.example.com) out of log files that looks like this:
http://maps.google.com/maps?hl=en&tab=wl
http://l.macys.com/simi-valley-ca?cm_mmc=macys_
https://www.google.co.in/
https://www.google.ca/
I want just
http://maps.google.com/
http://l.macys.com/
https://www.google.co.in/
https://www.google.ca/
Any ideas?
How about
URL url = new URL("http://maps.google.com/maps?hl=en&tab=wl");
System.out.println(url.getProtocol()+"://"+url.getHost());
Output
http://maps.google.com
If you don't want to handle it yourself, then a full proof way is following:
URL url = new URL("http://l.macys.com/simi-valley-ca?cm_mmc=macys_");
System.out.println(url.getProtocol() + "://" + url.getHost() + ((url.getPort()==-1)?"" : ":" + url.getPort()) + "/" );
You can skip url.getPort if you are sure that there will never be a port type url!!
Cheers

using hyperlinks in java file

I am trying to add a hyperlink in java file.
TestHyperlink.java
class TestHyperlink.java {
String url = "stackoverflow.com/questions/ask";
String someVariable = "testUrl";
Html entryLink = new Html("<a target=\"_blank\" href=url>someVariable</a>");
}
I am trying to use two string variables url and someVariable but I am not sure how to do it. My hyper link appears as 'someVariable' and on click leads to a broken page.
What I seek is a hyperlink which appears as testUrl and on click leads to a desired url page, stackoverflow.com/questions/ask in this case.
Thanks,
Sony
Java doesn't interpolate variables inside Strings. You need to change to new Html("<a target=\"_blank\" href=\"" + url + "\">" + someVariable + "</a>");

Categories