1

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:

  1. Firstly my program download website throught jsoup
  2. Then doing some parsing and editing code
  3. 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");
        }
    }
}

Solution To load CSS i changed 117 line:

mWebView.loadData(websites, "text/html", "UTF-8");

to this:

mWebView.loadDataWithBaseURL(url, websites, "text/html", "UTF-8", "");

And it's working great not.

5
  • 2
    Instead of loadData() try to use loadDataFromBaseUrl(). Commented Sep 17, 2014 at 10:52
  • String has good path to css stylesheet Please show path. Commented Sep 17, 2014 at 10:54
  • @greenapps i think it's good: 09-17 12:47:52.705: I/oldpath(19339): mywebsite.com/Content/Css/Combined/publish_1.5.3.98.css I'm trying to change loadData() to loadDataFromBaseUrl() Commented Sep 17, 2014 at 10:58
  • @greenapps you helped me, thanks a lot Commented Sep 17, 2014 at 11:52
  • @greenapps please post your suggestion as an answer so Volz can mark it and the thread can be closed. Commented Sep 18, 2014 at 4:53

1 Answer 1

0

Instead of loadData() try to use loadDataFromBaseUrl().

Well or do it With it ;-).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.