[英]Showing HTML page with CSS attached in a WebView
I'm trying to load a web page in a WebView
, but when the page is retrieved I lost all style information, maybe the CSS file is not used properly in my web view. 我正在尝试在WebView
加载网页,但是当检索页面时,我丢失了所有样式信息,也许CSS文件未在我的Web视图中正确使用。
For retrieving data I'm using HTTP POST
in this way: 为了检索数据,我以这种方式使用HTTP POST
:
public void postData() throws IOException, ClientProtocolException {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("param1", "parameter1"));
nameValuePairs.add(new BasicNameValuePair("param2", "parameter2"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL_STRING);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String data = new BasicResponseHandler().handleResponse(response);
WebView mWebView = (WebView) findViewById(R.id.wv);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadData(data, "text/html", "utf-8");
}
How can I solve this problem? 我怎么解决这个问题?
The problem is the relative path href="/maindir/css/sites.css"
: 问题是相对路径href="/maindir/css/sites.css"
:
Solution 1: (change at server) 解决方案1 :(在服务器上更改)
In case you have access to web-server try using full path in <link type="text/css" rel="stylesheet" href="/maindir/css/sites.css">
. 如果您可以访问Web服务器,请尝试在<link type="text/css" rel="stylesheet" href="/maindir/css/sites.css">
使用完整路径。 So instead of /maindir/css/sites.css
you will have http://your.domain.com/maindir/css/sites.css
因此,您将拥有http://your.domain.com/maindir/css/sites.css
而不是/maindir/css/sites.css
Solution 2: (change at client) 解决方案2 :(在客户处更改)
Instead of mWebView.loadData(data, "text/html", "utf-8");
而不是mWebView.loadData(data, "text/html", "utf-8");
use method loadDataWithBaseURL (String baseUrl, String data, String mimeType, String encoding, String historyUrl) the baseUrl
string will be used in order to make relative path to work. 使用方法loadDataWithBaseURL(字符串的baseUrl,字符串数据,字符串mime类型,字符串编码,字符串historyUrl)所述baseUrl
字符串将被以使相对路径来工作使用。
Result will be something like: 结果将是这样的:
mWebView.loadDataWithBaseURL("http://your.domain.com/", data, "text/html", "utf-8", null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.