android webview 远程 html如何加载本地js

html-css011

android webview 远程 html如何加载本地js,第1张

先把html文件下载到本地,然后更改html中js的路径为本地路径之后进行调用。

1.需要先写一个 LocalFileContentProvider

public class LocalFileContentProvider extends ContentProvider {

public static final String URI_PREFIX = "content://com.youpackage"//这里更改为你的包名

public static String constructUri(String url) {

Uri uri = Uri.parse(url)

return uri.isAbsolute() ? url : URI_PREFIX + url

}

@Override

public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {

File file = new File(uri.getPath())

ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)

return parcel

}

@Override

public boolean onCreate() {

return true

}

@Override

public int delete(Uri uri, String s, String[] as) {

throw new UnsupportedOperationException("Not supported by this provider")

}

@Override

public String getType(Uri uri) {

throw new UnsupportedOperationException("Not supported by this provider")

}

@Override

public Uri insert(Uri uri, ContentValues contentvalues) {

throw new UnsupportedOperationException("Not supported by this provider")

}

@Override

public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {

throw new UnsupportedOperationException("Not supported by this provider")

}

@Override

public int update(Uri uri, ContentValues contentvalues, String s, String[] as) {

throw new UnsupportedOperationException("Not supported by this provider")

}

}

2. 类似 jsUrl为网络端url jsPath为本地路径 data为html文件的字符串内容

data = data.replace(jsUrl, LocalFileContentProvider.URI_PREFIX+jsPath)

mWebView.loadDataWithBaseURL("http://yourwebsite", data, "text/html", "UTF-8", "")

这个是浏览器兼容的问题,可能原因:

浏览器的版本不同

跟分辨率也有关系,两天电脑的分辨率不同

这个是html网页,不用服务端交互的,是静态页面;所以根IIS没关系

关于css只要包含的路径没问题css加载正确,实质上css在网页内和外部都没关系的;(css放在外部比较符合代码规范,便于管理,如果css较多建议放在外边)

你这是css没做好浏览器兼容的问题,建议如果作网页就多下载几个浏览器

“怎么让网页能够放在任何地方都能正常打开呢?”要学好css,html和js;做好浏览器兼容