2011年3月6日日曜日

WebViewのloadDataメソッドおよびloadDataWithBaseURLメソッド(1)

WebViewのloadDataメソッドおよびloadDataWithBaseURLメソッドの仕様について、資料が
少ないので自分でできるだけ調べてみる。

尤も、JAVA始めて2ヶ月なので内容や用語の正確さに関して自信はないが。

1.リファレンスの翻訳
http://developer.android.com/intl/ja/reference/android/webkit/WebView.html より

loadData(String data, String mimeType, String encoding)
Load the given data into the WebView.
#WebViewにデータを読み込む。


loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl)
Load the given data into the WebView, use the provided URL as the base URL for the content.
#BaseURLを指定してWebViewにデータを読み込む。


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

Since: API Level 1
Load the given data into the WebView. This will load the data into WebView using the data: scheme. Content loaded through this mechanism does not have the ability to load content from the network.
#WebViewにデータを読み込む。このメソッドはWebViewにdata: scheme(data:スキーム)を使ってデータを読み込む。この方式でネットワーク上の内容を読み込むことはできない。
Parameters
dataA String of data in the given encoding. The date must be URI-escaped -- '#', '%', '\', '?' should be replaced by %23, %25, %27, %3f respectively.
#エンコードされたStringデータ。ただしデータはURIエスケープされていなければならない。('#'、'%'、'\'、'?'についてはそれぞれ%23、%25、%27、%3fに置き換えられている必要がある。)
mimeTypeThe MIMEType of the data. i.e. text/html, image/jpeg
#データのMIMEタイプ(text/html、image/jpegなど)。
encodingThe encoding of the data. i.e. utf-8, base64
#データのエンコード方式(utf-8、base64など)。

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

Since: API Level 1
Load the given data into the WebView, use the provided URL as the base URL for the content. The base URL is the URL that represents the page that is loaded through this interface. As such, it is used to resolve any relative URLs. The historyUrl is used for the history entry.
Note for post 1.0. Due to the change in the WebKit, the access to asset files through "file:///android_asset/" for the sub resources is more restricted. If you provide null or empty string as baseUrl, you won't be able to access asset files. If the baseUrl is anything other than http(s)/ftp(s)/about/javascript as scheme, you can access asset files for sub resources.
#BaseURLを指定してWebViewにデータを読み込む。BaseURLにはこのインターフェースで読み込まれるページのURLを指定する。それによって相対パスの使用が可能になっている、historyUrlは履歴の入力に使用される。
#注意 バージョン1.0以降、WebKitの変更により、"file:///android_asset/"によるassetフォルダ内のリソースへのアクセスは制限が厳しくなっており、baseUrlにnullやemptyを指定するとassetフォルダ内のリソースへはアクセス出来ない。baseUrlがhttp(s)/ftp(s)/about/javascriptなどのスキームで指定されていればリソースにアクセス可能である。
Parameters
baseUrlUrl to resolve relative paths with, if null defaults to "about:blank"
#相対パスの基準となるURL。nullの場合デフォルトの"about:blank"となる。
dataA String of data in the given encoding.
#エンコードされたStringデータ。
mimeTypeThe MIMEType of the data. i.e. text/html. If null, defaults to "text/html"
#データのMIMEタイプ(text/htmlなど)。nullの場合デフォルトの"text/html"となる。
encodingThe encoding of the data. i.e. utf-8, us-ascii
#データのエンコード方式(utf-8、us-asciiなど)。





2.リファレンスを読んで

WebViewでローカルのHTMLファイルを表示する で書いた、#や%が上手く処理できない件については、loadDataのリファレンスにこのように書いてある。(逆にloadDataWithBaseURLには書いていない⇛正しく処理できる、というのを読み取るのは難しい気はするが。)
これについては、リファレンスの記述に素直に従うならば、#等は%23等に置き換えた上でloadDataメソッドを使用するべきらしい。


読み込みデータの形式はdata: scheme(data:スキーム)を使用するとのこと。このスキーム自体ぼくは初めて目にするので、また調べる必要がある。

ちなみにloadDataWithBaseURLのほうにはdata: schemeを使用すると書いていないが、そこの仕様は同じと思っていいんだろうか。
また、loadDataメソッドのmimeTypeはやはりnullの場合デフォルト値"text/html"になるのだろうか。

#2011/3/11追記 loaddata-Parameters-data の訳文が変だったので少し直した。The date must be~ は The data must be~ のミスタイプだと思う。

0 件のコメント:

コメントを投稿