我正在使用jsoup下载和更改网站,然后在WebView中显示它。我的问题是css没有正确加载。我已尝试删除this issue中的doctype标记,但没有帮助。我必须补充一点,下载的网站有很好的样式表路径。
关于我的代码:
我发现通过在PC上下载相同网站并在没有css样式表标签的情况下启动它是css问题。
这是我做上述事情的java类:
package com.example.sqllite;
import java.io.IOException;
import java.util.List;
import com.example.kontaktysqllite.R;
import android.os.AsyncTask;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.DocumentType;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;
/**
* Okno danego szablonu (webview)
*
* @author Alan
*
*/
public class TemplateActivity extends Activity {
private WebView mWebView;
private String url;
private StringBuffer website;
private String websites;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.template_activity);
// Webview
this.url = getIntent().getExtras().getString("urlString");
new ParseTask().execute(getIntent().getExtras().getString("urlString"));
}
private void addHtml(String html) {
website.append(html);
}
private void getHtml() {
String html = this.website.toString();
websites = html;
}
private class ParseTask extends AsyncTask<String, Void, Document> {
@Override
protected Document doInBackground(String... arg0) {
Document doc = null;
for (String url : arg0) {
try {
doc = Jsoup.connect(url).get();
} catch (IOException e) {
e.printStackTrace();
}
}
return doc;
}
@Override
protected void onPostExecute(Document doc) {
List<Node>nods = doc.childNodes();
nods.get(0).remove();
Elements tekst = doc.getElementsByClass("ctr-textTitle");
for (Element div : tekst) {
for (Element child : div.children()) {
for (Element child2 : child.children()) {
for (Element child3 : child2.children()) {
for (Element child4 : child3.children()) {
child4.text("heheh");
}
}
}
}
}
Elements imports = doc.select("link[href]");
for (Element link : imports) {
//if (link.attr("rel") == "stylesheet") {
String path = link.attr("abs:href");
String newpath = url + path;
Log.i("oldpath", path);
Log.i("path", newpath);
//link.attr("abs:href", newpath);
//}
}
String html = doc.html();
websites = html;
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.setWebViewClient(new WebViewClient());
// getHtml();
Log.i("task", "" + websites);
mWebView.loadData(websites, "text/html", "UTF-8");
}
}
}
解决方案 要加载CSS我更改了117行:
mWebView.loadData(websites, "text/html", "UTF-8");
到此:
mWebView.loadDataWithBaseURL(url, websites, "text/html", "UTF-8", "");
而且效果不好。
答案 0 :(得分:1)
而不是loadData()
尝试使用loadDataFromBaseUrl()
。
好吧或做With
它; - )。