I'm using jsoup to downloading and changing website and then displaying it in WebView. My problem is that css is not loading properly. I've tried deleting doctype tag like in this issue but not helped. I must add that downloaded website has good paths to stylesheet.
Something about my code:
- Firstly my program download website throught jsoup
- Then doing some parsing and editing code
- Saving html to String and sending it to WebView. String has good path to css stylesheet so I don't know what cause this problem.
I found that it is css problem by downloading same site on PC and launching it without css stylesheet tag.
Here's my java class for doing above stuff:
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");
}
}
}
0 comments:
Post a Comment