Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Android webview's loadData and loadDataWithBaseURL

The Android webview has 2 methods to load data

public void loadData (String data, String mimeType, String encoding)

Note that JavaScript's same origin policy means that script running in a page loaded using this method will be unable to access content loaded using any scheme other than 'data', including 'http(s)'. To avoid this restriction, use loadDataWithBaseURL() with an appropriate base URL.

and

public void loadDataWithBaseURL (String baseUrl, String data, String mimeType, String encoding, String historyUrl)

Note that content specified in this way can access local device files (via 'file' scheme URLs) only if baseUrl specifies a scheme other than 'http', 'https', 'ftp', 'ftps', 'about' or 'javascript'.

I don't know what do these 2 sentences mean and when to select between the two ?

Thanks in advance

like image 514
onmyway133 Avatar asked Sep 14 '12 08:09

onmyway133


People also ask

What is loadUrl?

loadUrl when you say loadUrl. From documentation, only difference between them is, loadURL renders a webkit having a url that you set. On the other hand, loadData renders webkit, that source code comes from a parameter, and baseURL is also a parameter.

How to open url in WebView Android?

Launch an url in an external browser app from your app. startActivity(new Intent(Intent. ACTION_VIEW, Uri.

What is WebView in Android studio?

The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout. It does not include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebView does, by default, is show a web page.

How to load WebView in Android?

Modify src/MainActivity. java file to add WebView code. Run the application and choose a running android device and install the application on it and verify the results. Following is the content of the modified main activity file src/MainActivity.


2 Answers

public void loadData (String data, String mimeType, String encoding)

In this we pass the HTML, mimeType and encoding

where else in

public void loadDataWithBaseURL (String baseUrl, String data, String mimeType, String encoding, String historyUrl)

where baseUrl could be the base url such as the path to asset folder, or SDCard or any other path, where your images or the other media resides related to your html, and I am not much aware of historyUrl

accoring to the docs of [loadData][1]

Note that JavaScript's same origin policy means that script running in a page loaded using this method will be unable to access content loaded using any scheme other than 'data', including 'http(s)'. To avoid this restriction, use loadDataWithBaseURL() with an appropriate base URL.

means loaddata will only include the part which is resides in the first parameter.

and

Note that content specified in this way can access local device files (via 'file' scheme URLs) only if baseUrl specifies a scheme other than 'http', 'https', 'ftp', 'ftps', 'about' or 'javascript'.

simple meaning of above is you can access the data from http and... other by passing the baseUrl

for example I have written HTML which required tons of image from my ftp or other place what I would do is pass the url of my ftp in baseURl parameter and I can access to my images

like image 132
Nixit Patel Avatar answered Oct 01 '22 14:10

Nixit Patel


The second one comes in handy when you're loading the HTML locally and it references assets such as images & css which are also packaged locally

like image 39
Philippe Girolami Avatar answered Oct 01 '22 15:10

Philippe Girolami