1

I'm trying to load a website in a WebView and it loads the source code just fine. However i cannot seem to get it to render the website, instead it displays the source code.

I've tried to change the content-type of the returning response on the server-side but no matter what type is indicated only text/plain seems to work in the actual WebView (the WebView disregards the content-type, if I dump the response text/html is indeed implied).

Here is my code;

HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "Basic " + Base64.encodeToString((Application.getInstance().getUserName() + ":" + Application.getInstance().getUserPassword()).getBytes(), Base64.NO_WRAP));

String url = getIntent().getStringExtra("url");
WebView webView = ((WebView) findViewById(R.id.ledger_webview));
webView.getSettings().setJavaScriptEnabled(true);
webView.clearCache(true);
webView.loadUrl(url, headers);

From what I can tell other people use the same method, I've also looked in the manual which basically says to do the steps I'm doing, but obviously I'm missing something here.

Do I need to pass a flag somewhere, or manually indicate the content-type for the WebView?

Edit: This is the response headers from the server

Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 2559
Content-Type: text/html
Daniel
  • 3,504
  • 4
  • 23
  • 43
  • To load a URL, I simply use this: `webview.loadUrl("http://www.google.com"); // Example URL` – Phantômaxx Aug 22 '16 at 09:52
  • @Rotwang Ok, so I tried to load the url given your line, and what happens is that the website is opened in a new window (not limited to google.com), in the chrome browser, instead of in the actual WebView. – Daniel Aug 22 '16 at 09:56
  • See this answer: http://stackoverflow.com/a/12802173/2649012 – Phantômaxx Aug 22 '16 at 10:17
  • @Rotwang Still displays the source code instead of actually rendering it. – Daniel Aug 22 '16 at 10:49
  • That's weird, indeed. Are you still using the first who lines (those which define the "headers")? – Phantômaxx Aug 22 '16 at 10:56
  • 1
    @Rotwang I've tried both with and without them. I'll let the question stand, but I've created a service instead that downloads the HTML and then loads it into the WebView through the `webView.loadData()` method, which works. – Daniel Aug 22 '16 at 11:08
  • Nice workaround. Possibly, add it as your own answer. – Phantômaxx Aug 22 '16 at 11:09

0 Answers0