简体   繁体   中英

Loading html data from page source into webview

I'm trying to load a page in my webview by copying the html source (right click page, select 'View page source') and pasting it into my HTML file that I have in my android's 'assets' folder.

The problem I encounter is that when I use webview.loadUrl(myUrl); it loads the page but it's mostly black and white, I dont see any colors.. usually only the links show (the links are clickable but don't work, they just give me a Webpage not available error) and thats about it.

How would I fix this behavior?

my code looks like this

@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        WebView webView = (WebView)findViewById(R.id.myWebview);

        webView.getSettings().setJavaScriptEnabled(true);


        webView.setWebViewClient(new WebViewClient(){   
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url){
                view.loadUrl(url);
                return false;
            }
        });


        String myUrl = "file:///android_asset/Assets.html";
        webView.loadUrl(myUrl);


    }

the Assets.html file contains all the HTML code obviously.

I found the problem.

It was the encoding.

Unfortunately I am not sure what encoding to use but this is how I solved it.

When I copied the source from a different web page, and tried to run my app, an error showed up, it said that certain characters contained in the HTML source needed a different encoding to run, as soon as I selected "Change encoding" within the message that popped up, and ran the app, the website loaded fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM