Web View in Android - android

In my android app whole web page is loading in web view to which i gave the link, but I want to load only some component from that web page not rest of the component. Can anybody tell me is it possible to load only required component from web page and how?

It can be done using Jsoup html parsing library.
1.Get the data from url to jsoup document.
Document doc = Jsoup.connect("http://example.com/").get();
remove the unwanted content html tag using remove().
doc.select("span[style*=display:none]").remove(); //put unwanted tag inside.
Save the remaining content to a string.
String newcontent=doc.toString();
4 Set the newcontent as Webview content.
webView.loadDataWithBaseURL(null, newcontent, "text/html", "UTF-8", null);
Sorry for poor english.

I dont know if there is a standard api to do this, but if not you could load the html first then manipulate it. So you could delete the areas you dont need and only display the necessary parts.
Cheers

Related

How to scroll Android WebView programmatically to a specific element

I load an HTML string directly into a WebView with loadDataWithBaseURL(null, html, null, null, null) and later I'd like to be able to scroll programmatically to an internal element, e.g. <p id="pos1"> or <a name="pos1"></a>
I found a similar question, where the answer was to use javascript:
https://stackoverflow.com/a/2239301/2534762
But is that really the only way? Isn't there another simple way to say directly to a WebView: "go to #pos1" without having to embed a script in every HTML page that you want to scroll? It seems to me a common task begging for a simple solution, but apparently I'm wrong... or is there a way to do that, maybe calling loadUrl or loadDataWithBaseURL in some particular way?
I found that I don't need to embed JavaScript in the HTML page, as suggested in that other answer, I can instead write one line of JavaScript directly in the URL:
webView.loadUrl("javascript:document.getElementById('"+aID+"').scrollIntoView()");

Find language of a site loaded in my webview

I have a webview where external url will be launched and loaded in the webview.
So, I want to find what language the webpage is? is it possible in android webview?
After scanning the documentation it doesn't look like there's an explicit method in the WebView class that allows you to retrieve the language used by the web page.
An alternative way of finding out the language of a web page would be to download the raw HTML content of the page using the JSoup library and parsing it for the lang attribute. However, this would obviously require that the owner of the page specified that attribute when creating the page.
Here's a snippet of how you can use JSoup to get the raw HTML content of the page you're trying to load:
Document htmlDoc = Jsoup.connect("http://en.wikipedia.org/").get();
Then, to parse for the lang attribute (which is used to specify the natural language of the content found on the web page), you would do this (credit to this OP):
Element html = htmlDoc.select("html").first();
String langAttr = htmlDoc.attr("lang"); //check that this isn't null before using it
Hope this helps. But to answer your original question - no, the WebView class doesn't have an explicit method to obtain the language of a web page included out of the box.

How to use an XML data from an URL link?

The title maybe a little confusing so I shall explain a lot more details here. First off, I have an URL link that uses HTTPS secured site. I already managed a successful attempt to use a WebView to connect to the site, using loadDataWithBaseURL method.
The URL link does not end with .xml, so I am stuck with this problem. What are the procedures to do in order I can use the xml data on an URL link that does not ends with .xml?
EDIT:
<MGMT>
<NET>
<HEAD>
<ClientID>99999999</ClientID>
<ServerID>WEB_01</ServerID>
<Rsp>00</Rsp>
<Auth></Auth>
</HEAD>
<STAT>
<IP>192.168.5.158</IP>
<Status>OK, Success!</Status>
</NET>
</MGMT>
I do have XML parsing knowledge. But that is if the url link returns a XML data or I have an XML file. This is NOT THE ACTUAL LINK but it is similar, I changed a few values in it.
https://192.168.0.254/?ClientID=999999&Cert=0f7a248e3b017effec2b36cf53912b0f&IP=192.168.5.128
Ok. As i come to know about your question, you have to gain some knowledge about xml parsing which convert the fetched XML data in to specific variables.
Here in below examples, the project get the Http responce from the server which is in the formate of the xml file. (Which URL not having .XML extention). And that data are stored in to specific variable.
You have to fetch various data based on the tab as per your xml structure. as like "MGMT", "NET" . . .etc etc. . .
See this: Example 1
Also see this: Exapmple 2
Hope you got the Logic to do it.

Determine element of url from webview shouldOverRideUrlLoading

I am displaying a html in a custom activity with a webview. I am overriding
public boolean shouldOverrideUrlLoading(WebView view, String url) {
to intercept certain urls. Now I would like to be able to figure out some more details about the html around that url.
In my case I would like to get the html element that contains the url. E.g. if the url comes from
Manfred Moser
I would like to be able to somehow retrieve the value "Manfred Moser" from within the anchor tag. Is there a way to do that. I found that you do not have access to the DOM from Java.
One way I can think of would be to download the page separately and parse it all before even loading it in webview. However that is an ugly hack at best. Is there a better way?
There is a dirty hack:
Bind some Java object so that it can be called from Javascript with WebView:
addJavascriptInterface(javaObjectExposed, "JSname")
Force execute javascript within an existing page by
WebView.loadUrl("javascript:window.JSname.passData("some data");");
Described here: http://lexandera.com/2009/01/extracting-html-from-a-webview/
Note: this is a security risk - any JS code in this web page could access/call your binded Java object. Best to pass some one-time cookies to loadUrl() and pass them back your Java object to check that it's your code making the call.
In addition to #Peter Knego's approach, there is the inverse:
Use addJavascriptInterface(), per his step #1
For your link, use an onClick attribute on your <a> tag to call out some method on the Java object from step #1, where the Java object turns around and loads the URL into the WebView, and supplies whatever sort of identifying information you want.
Suffice it to say, there's no way to get the information you want from shouldOverrideUrlLoading().

problem in display image with loadDataWithBaseURL() in Android

In my Application, I display data in webview but it can't display the images in the webview.
I used loadDatawithBaseURL() method.
This is my code..
webview.loadDataWithBaseURL("file:///059600656X/", data, "text/html", "UTF-8", "about:blank");
// here data is a string object which contain html parsing data.
I think it cant find the images in given directory.
Can Anybody help me?
You cannot use simple file:/// URLs with loadDataWithBaseURL(), and "about:blank" probably is not a valid base URL.
To make the loadDataWithBaseURL work you need to make sure of two things:
All image src include "file:///android_asset/imagename"
and the images are in the asset folder!!
So contrary to answer1, it is as simple as that!
Munir Syed
When you use LoadDataWithBaseURL you must specify the image resource as ur baseurl and data must be ur array or image renderer.
example
wv.loadDataWithBaseURL("file:///android_asset/", srray[position], "text/html", "UTF-8", null);
this should help me out

Categories

Resources