0

I am working on an Android application using Xamarin but hopefully the question below is more general and can be answered also by people not familiar with Xamarin. The app is very simple and has one main page with a list where the use can click on any of the items. These items represent products and all the related information are stored into a dictionary (key is the product name, values are some information about the product). Amongst the available fields, I also added a URL of the website where the user can access more information about the product. The app has a WebView to display the page inside the app.

I can display the webpage by using:

web = FindViewById<WebView>(Resource.Id.webView);
web.SetWebViewClient(new myWebViewClient());
web.Settings.JavaScriptEnabled = true;
web.LoadUrl(item.Link);

where the item/product the user has selected is the item object having the URL saved in item.Link. The problem is that the user need a web connection in order to load the page. So I thought about saving the whole HTML code into a string so that the user can visualize the content even if no connection is available. I am doing this by:

WebClient wc = new WebClient();
using (Stream st = wc.OpenRead(web.Url))
{
   using (StreamReader sr = new StreamReader(st, Encoding.UTF8))
   {
      html = sr.ReadToEnd();
   }
}

Then each item in the dictionary will have a field item.Html with the source code of the page. I can visualize the page with the following code:

web = FindViewById<WebView>(Resource.Id.webView);
web.SetWebViewClient(new myWebViewClient());
web.Settings.JavaScriptEnabled = true;
//web.LoadUrl(item.Link);               
web.LoadData(item.Html, "text/html", "UTF-8");

The problem is that the viewer does not display the page in the same way as web.LoadUrl(item.Link) would do (i.e. the page as you would see in any web browser) but it is much messier and difficult to navigate. What am I doing wrong? Any better way to achieve my goal?

opt
  • 477
  • 1
  • 10
  • 25
  • you probably need to set the baseurl - see https://stackoverflow.com/questions/29360936/what-is-baseurl-in-android-web-view – Jason Dec 09 '17 at 16:00
  • The problem with this is that the base URL still need web access. I tried and in case of no connection available, that method will not work. – opt Dec 09 '17 at 16:09
  • You would need to download and cache all of the external resources (images, css, js, etc) used by the page – Jason Dec 09 '17 at 16:23
  • Is there a way to do it in Xamarin? – opt Dec 09 '17 at 16:55
  • Sure - parse the HTML file, extract the urls of any resources, download them, save locally, etc. It's the same process a web browser uses to cache content. – Jason Dec 09 '17 at 17:03
  • This sounds a bit complicated. I was wondering if there is any available solution (either when using the webview in Xamarin or with another library I can still use in a Xamarin app) I can use for the purpose of caching the webpage and load it back when needed in the very same format as you see when you load the page online. – opt Dec 10 '17 at 10:52
  • not that I'm aware of – Jason Dec 10 '17 at 16:21
  • [Make sure the html which you got is right format (java code)](https://stackoverflow.com/questions/8200945/how-to-get-html-content-from-a-webview), you can copy it to your note, and change it to `.html`, and open it to see if it's right html format. – Robbit Dec 11 '17 at 08:43

0 Answers0