當前位置:編程學習大全網 - 源碼下載 - android loaddatawithbaseurl 和loadurl的區別

android loaddatawithbaseurl 和loadurl的區別

在開發Android平臺的互聯網應用時,經常會使用到WebView,好處主要有兩個,壹是可以更改要展現的內容(包括樣式),二是可以實現部分功能的跨平臺。

Android的WebView組件使用非常簡單,可以使用loadUrl()加載壹個Url地址,也可以使用loadData()或 loadDataWithBaseURL()加載壹段HTML代碼片段。loadUrl()的使用大家應該都沒有什麽問題,但是loadData()和 loadDataWithBaseURL()在使用上的差異可能有些人還不太清楚。

首先,從方法的定義來看:

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

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

loadDataWithBaseURL()比loadData()多兩個參數,可以指定HTML代碼片段中相關資源的相對根路徑,也可以指定歷史Url。兩個方法的其余三個參數相同。

其次,兩個方法加載的HTML代碼片段有些不同,loadData()中的html data中不能包含'#', '%', '\', '?'四中特殊字符,這就為我們內嵌css等制造了些許麻煩,因為css中經常用'#', '%'等字符,需要如何處理呢?我們需要用UrlEncoder編碼為%23, %25, %27, %3f 。

/**

* Loads the given data into this WebView using a 'data' scheme URL.

* <p>

* 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 {@link

* #loadDataWithBaseURL(String,String,String,String,String)

* loadDataWithBaseURL()} with an appropriate base URL.

* <p>

* If the value of the encoding parameter is 'base64', then the data must

* be encoded as base64. Otherwise, the data must use ASCII encoding for

* octets inside the range of safe URL characters and use the standard %xx

* hex encoding of URLs for octets outside that range. For example,

* '#', '%', '\', '?' should be replaced by %23, %25, %27, %3f respectively.

* <p>

* The 'data' scheme URL formed by this method uses the default US-ASCII

* charset. If you need need to set a different charset, you should form a

* 'data' scheme URL which explicitly specifies a charset parameter in the

* mediatype portion of the URL and call {@link #loadUrl(String)} instead.

* Note that the charset obtained from the mediatype portion of a data URL

* always overrides that specified in the HTML or XML document itself.

*

* @param data a String of data in the given encoding

* @param mimeType the MIME type of the data, e.g. 'text/html'

* @param encoding the encoding of the data

*/

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

checkThread();

mProvider.loadData(data, mimeType, encoding);

}

/**

* Loads the given data into this WebView, using baseUrl as the base URL for

* the content. The base URL is used both to resolve relative URLs and when

* applying JavaScript's same origin policy. The historyUrl is used for the

* history entry.

* <p>

* 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'.

* <p>

* If the base URL uses the data scheme, this method is equivalent to

* calling {@link #loadData(String,String,String) loadData()} and the

* historyUrl is ignored.

*

* @param baseUrl the URL to use as the page's base URL. If null defaults to

* 'about:blank'.

* @param data a String of data in the given encoding

* @param mimeType the MIMEType of the data, e.g. 'text/html'. If null,

* defaults to 'text/html'.

* @param encoding the encoding of the data

* @param historyUrl the URL to use as the history entry. If null defaults

* to 'about:blank'.

*/

public void loadDataWithBaseURL(String baseUrl, String data,

String mimeType, String encoding, String historyUrl) {

checkThread();

mProvider.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);

}

  • 上一篇:學習計算機網絡編程應該學些什麽?
  • 下一篇:薅羊毛什麽意思
  • copyright 2024編程學習大全網