在WebView中显示附加了CSS的HTML页面

GVi*_*i82 1 css android webview

我正在尝试在a中加载一个网页WebView,但是当检索到页面时,我丢失了所有样式信息,可能在我的Web视图中没有正确使用CSS文件.

为了检索我正在使用的数据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");
}
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?

mad*_*mad 5

问题是相对路径href="/maindir/css/sites.css":

解决方案1 ​​:( 在服务器上更改)

如果您有权访问Web服务器,请尝试使用完整路径<link type="text/css" rel="stylesheet" href="/maindir/css/sites.css">.所以/maindir/css/sites.css你没有http://your.domain.com/maindir/css/sites.css

解决方案2 :(在客户端更改)

而不是mWebView.loadData(data, "text/html", "utf-8");使用方法loadDataWithBaseURL(String baseUrl,String data,String mimeType,String encoding,String historyUrl),baseUrl将使用该字符串以使相对路径起作用.

结果将是这样的:

mWebView.loadDataWithBaseURL("http://your.domain.com/", data, "text/html", "utf-8", null);