i load a web page in a webview that contains a form, i want to know if there is a way to save the data that user put in the form in the webview cache and auto fill it in his next visit?
this is all i have done for now.
webView.getSettings().setAppCachePath("/data/data/"+ getPackageName() +"/cache");
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
not sure if it's the right way but, what about running js script on webview to get form input values, navigating DOM tree of html or using selector with id ?
once you got them you can store them in your app and for repopulate, before loading the page in webview, you manipulate html (if you could) or you use another js script to set the values
i'm not sure but i thing cache is only for cachable values (images, css...) not for input
Related
I have a web site that I stored locally into myProject/assets . I want to create a WebView to show it.
I use this code:
myWebView.loadDataWithBaseURL("file:///android_asset/", htmlContent, "text/html", "utf-8", null);
Where htmlContent is a string and it is correctly loaded from the local files.
Unfortunately, the site has absolute links: they all start with a "/" and when such a link is clicked by the user, I get this error in the WebView:
file:///linkedpage.html not found
So, as the documentation says, only relative links and not absolute links are preceded by the BaseURL. Indeed, if I remove the initial "/" from the links, it works.
This can be an error of how the site was written or not, but I'm not the writer of it and I cannot change it (and anyway it makes sense, because the html links are actually in the root of the site, so it's not wrong to call them with a "/".)
How can I manage this? Is there a way to intercept clicks on the links and modify their url, for example? Or can I effectly teach the WebView that "file:///android-asset/" is really the root of the site, and not "file:///", apache-style? Or is there another cleaner solution (besides changing the html file, that I don't want to do)
I have the URL of a webpage to be displayed into a webview in my Android app. Before showing this page i want to clear the html code of this page from some tag (such as the header, footer, ecc..) in order to show only few information. How can i do it? I tried to solve the issue working with JSoup but i can't understand how to create and pass the "new page" to the webview. Anybody can help me?
EDIT
I cleaned the html code useless through jsoup libraries. Then, always by mean of these, i get head and body content and finally i showing the "cleared" web page through these lines:
headURL = doc.select("head").outerHtml();
bodyURL = doc.select("body").outerHtml();
webview.loadData( "<html>"+headURL+bodyURL+"</html>" , "text/html", "charset=UTF-8");
webview.setWebViewClient(new DisPlayWebPageActivityClient());
The view shows the new page but do not load css files specified in the head(that has not been touched). Who can say me why?
You can fetch the WebPage you want to display as a string, parse and remove whatever you don't want and then load this string as data in your webview.
Something like:
String webContent = fetchPage(url);
String cleanedWebContent = cleanUp(webContent);
webView.loadData(cleanedWebContent, "text/html", "UTF-8");
Of course, you will need to implement fetchPage and cleanUp as they are not Android methods
When I try to reload the titanium webview true webview.reload(), the view does not reload correctly. Instead if loading the page it gives me a page not found.
what i'm doing:
In Titanium i make use of webviews to display data. These webviews make use of HTML that is stored in the local filesystem that Titanium offers. The webview is called url is set by :
webview.setUrl( Ti.Filesystem.applicationDataDirectory.toString() + 'index.html');
This sets the proper url for the webview, it let's me see the correct html page. When I use webview.reload(), it seems lost... is there a way to reload the webview, or should i remove and then add the webview again?
Setting a URL for WebView the resource is usually loaded from the Resources folder.
So try to move all HTML files there (into Resources, same folder where app.js is located) and simply use.
webview.setUrl('index.html');
This has worked for me both on iOS and Android.
(There is an issue related to Android regarding WebView and setting its content by html property but this shouldn't matter here)
I want to get images from the web page and save them into the local storage. I can find all image URLs from the HTML, load that images from the server and save them. But I don't want to access to the server second time for the same information which is already in webview. So I want to know how I can load images from the webview?
You can set a WebView cache in a few steps. Take a look below.
WebView webView = (WebView) findViewById(R.id.your_webView_Id);
String cacheDir = getDir("your_WV_cache_dir", Context.MODE_PRIVATE).getAbsolutePath();
webView.getSettings().setAppCacheMaxSize(1024 * 1024 * 1); // 1 Mb cache limited size > webView.getSettings().setAppCachePath(cacheDir);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); // This constant is important to you. This way you can load daa from cache, if it was already downloaded once.
You might take a look to th other possible values to the constant above at the WebView.WebSettings official documentation.
Let me know if it works.
when i try to get the html source code from webview i use this example for my solution:
Extracting HTML from a WebView
now i want to parse a page which has no html content. the page displays only a text extracted from xml source.
does anybody have an idea how to get the content (or xml source code) from webview?
best regards
EXAMPLE: XML
< ID >test< /ID > <BR>
< Status >0< /Status >
Is shown as: test0 in webview
I want get the "test0" and put it to string
Option #1: Use the same approach as is shown in the linked-to blog post. Devise some JavaScript that grabs the data out of your Web page and call that JavaScript via loadUrl(), routing the results to some JavaScript interface you injected into the WebView via addJavascriptInterface().
Option #2: Don't use WebView at all, and use HttpURLConnection or HttpClient to fetch the XML source.