我想在webview中加一个文本页面,但是当页面检索时,我丢失了所有的样式信息,有可能CSS文件不能在webview中正确使用。
为了检索数据,我使用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");
}
如何解决这个问题?
在您的代码中,您使用了HTTP POST请求从服务器检索HTML数据并将其加载到WebView中。如果您的HTML页面丢失了样式信息,则可能是因为CSS文件不能在WebView中正确使用。
为了解决这个问题,您可以尝试以下几种方法:
1.在HTML文件中直接引用CSS文件。例如:
<link rel="stylesheet" type="text/css" href="styles.css">
2.您可以尝试在WebView加载数据之前,先加载您的CSS文件。例如:
mWebView.loadData(data, "text/html", "utf-8");
3.您可以把整个HTML样式写入页面内
<style>...</style>
4.您可以使用baseUrl参数的loadData方法, 这样可以正确加载相对路径下的资源文件,例如:
mWebView.loadDataWithBaseURL( "file:///android_asset/",data, "text/html", "utf-8", null);
这些方法中的一种或多种都可能有助于解决问题。请尝试并看看哪种方法最适合您的情况。