Android webview giving blank screen when loading data from HTTPS - android

I have a problem with an URL that starts with https. It's showing a blank white screen whereas links, starting with http, loads successfully in Android webview.
With logs I'm sure that data is coming from HTTPS also but is not displaying in the view. Please help me. Some sample code or link will be most appreciated.

HI,
I read your question, I don't know what is your exact issue if you have data then you can use these line of code to display it in webview
encoding contains html tags like <p> aadfasdf asdf ds </p>
public static final String HTML_FORMAT =
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">" +
"<html><head><title></title>"+
"<style type=\"text/css\">" +
"'body {color:#FFFFFF;background-color:#000000;font-size: 10pt}'" +
"</style>" +
"</head>" +
"<body>" +
"%s" +
"</body></html>";
String textHtml = String.format(Constants.HTML_FORMAT,encoding);
wv.setBackgroundColor(Color.BLACK);
wv.loadDataWithBaseURL(null, textHtml, "text/html","utf-8", null);
try this hopefully you will get your required result.

Related

Web view problems

I have some problem in my web view.
I want to load a string from a WordPress blog, witch have html tags such as <a> and image tag and ... .
So my problems are:
As I mention above, I want to load a local string and I want to handle user click on the links, so I load data like this into the web view:
WebView webview = (WebView) this.findViewById(R.id.mainWV);
webview.setWebViewClient(new MyWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
String s="<p>It will enable Seattle-based Alaska to expand into lucrative hubs such</p>\n<p><img class=\"aligncenter size-full wp-image-1035\" src=\"http://ichef.bbci.co.uk/news/660/cpsprodpb/D09F/production/_89070435_89069565.jpg\" alt=\"\" width=\"300\" height=\"120\" /></p>\n<p>as San Francisco and Los Angeles.</p>\n";
webview.loadDataWithBaseURL("", s, "text/html", "utf-8", "");
and another way I tried was:
String head1 = "<head></head>";
String text = "<html>" + head1
+ "<body dir=\"rtl\" >" + s
+ "</body></html>";
webview.loadData(text, "text/html", "utf-8");
and my client is :
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("USER_CLICKED", url + "USER_CLICKED");
return true;
}
}
Ok, now when I run the app, and when I click on <a> I never see 'USER_CLICKED', but the webview content change and it seems web view is empty, I mean is white as snow.
notice 1: when I try this:
webview.loadUrl("https://android-arsenal.com/");
and run the app, when I click on the links in the loaded web view, every thing is OK, and i see this Log: 'USER_CLICKED' and the related URL.
notice 2: yes i try a lots of different URL, but loading from string, nothings change in click handling.
notice 3: I test in android 5.1 and 4.1 in 4.1 clicked recognized and is see 'User.. but in the 5.1 the white page story happens.(Edit: android 6 also do not show 'USER... ')
my number 2 problem is, when I call this:
webview.loadDataWithBaseURL("", s, "text/html", "utf-8", "");
the image tag does not load! I mean it is just ignore to load the images, and I do not know why.
notice 3: when I copy text from inside the web view there is some rectangle in the text.
OK every one, after a long time, i find the problem, so as i mentioned above, i take string from WordPress rest API(json), so in my word, the string should be OK, but as i find out, there is some extra '\' in the string, and the string is like:
<p>It will enable Seattle....
, as simple it is i just use:
s=s.replaceAll("\\","");
so, thank any one who see this post.
A URL is a URL. The first item is HTML, not a URL. If you want to load the URL then you have to pass a valid URL (not HTML) and do not expect the URL to be magically parsed out of the HTML string that you are loading. In fact the first item is not even valid HTML, it is a part of HTML, potentially a snippet, but it's not even enclosed in the HTML tags that I expect a web view would need.

How to get editable web view content to string in Android.?

I want to implement a Rich text editor by using a webview in android. Content can be loaded by using a HTML file (which resides in assets) without any problem. But if user has edited the content of the webview (with all the formatting), I need to get the modified content as a HTML string and save it in the database. How can I do this?
I tried in many ways but it seems that we need to pass a URL to get the content of the webview. But after editing the webview content, how can we get the edited URL? or current updated webview content to HTML formatted string?
Using below code I made editable web view.
String msgBody = "<html>\n"+
"<body>\n"+
"<div id=\"content\" contenteditable=\"true\" style=\"font-family:Helvetica;font-size:14px\">" + a +" </div>\n"+
"</body>"+
"</html>";
// wbview = (WebView)this.findViewById(R.id.wbview);
wbView.getSettings().setJavaScriptEnabled(true);
wbView.loadDataWithBaseURL("", msgBody, "text/html", "UTF-8", "");
wbView.setHorizontalScrollBarEnabled(true);
wbView.setVerticalScrollBarEnabled(true);
In iOS we can get it easily by using below code line.
NSString* html=[_tbEmail.webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('body')[0].innerHTML"];

Android: Unable to load simple html data having javascript file in WebView?

WebView web_view = (WebView) findViewById(R.id.webView1);
web_view.getSettings().setJavaScriptEnabled(true);
web_view.getSettings().setPluginsEnabled(true);
web_view.getSettings().setAllowFileAccess(true);
String data;
data = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
+ "<html>"
+ "<head>"
+ "<title>My First chart using FusionCharts XT</title>"
+ "<script type=\"text/javascript\" src=\"FusionCharts.js\">"
+ "</script>"
+ "</head>"
+ "<body>"
+ "<div id=\"chartContainer\">FusionCharts XT will load here!</div>"
+ "<script type=\"text/javascript\">"
+ "FusionCharts.setCurrentRenderer(\"javascript\");"
+ "var myChart = new FusionCharts(\"FusionCharts/Line.swf\", \"myChartId\", \"400\", \"300\", \"0\", \"1\" );"
+ "var dataString =\"<chart> <set label='0.00' value='0'/><set label='5.00' value='2' /><set label='7.00' value='3' /><set label='9.00' value='4' /><set label='12.00' value='2' /></chart>\"; "
+ "myChart.setXMLData(dataString);"
+ "myChart.render(\"chartContainer\");" + "</script>"
+ "</body>" + "</html>";
Log.i("info", "Html " + data);
web_view.loadData(data, "text/html; charset=UTF-8",null);
In my project i am using fusion charts. I am making a html string data and load it in WebView as in above code sample. When i run this html file in browser it runs and make me Fusion charts, But when i am doing this with for android Web view it is not loading in my Web View.
I have already Enable javascript.
I paste javascript file in to assets folder.
First of all, the browser you mentioned is a browser on your device or is it a browser in a computer?
Now, if it worked in the android browser (or a browser in your device), then the problem is because the javascript files are on your assets folder. When you indicate a resource through relative path (the way you are using), the webview searchs it relative to the same folder as your html file is. Since you are using a String as your "html file", I would recommend using the loadDataWithBaseURL(). I made an usage example below using the assets folder as the base URL, try it.
web_view.loadDataWithBaseURL("file:///android_asset/", data, "text/html", "UTF-8",null);
public void loadDataWithBaseURL (String baseUrl, String data, String mimeType, String encoding, String historyUrl)
Added in API level 1
Loads the given data into this WebView, using baseUrl as the base URL for the content. The base URL is used both to resolve relative URLs and when applying JavaScript's same origin policy. The historyUrl is used for the history entry.
Note that content specified in this way can access local device files (via 'file' scheme URLs) only if baseUrl specifies a scheme other than 'http', 'https', 'ftp', 'ftps', 'about' or 'javascript'.
If the base URL uses the data scheme, this method is equivalent to calling loadData() and the historyUrl is ignored.
Parameters
baseUrl the URL to use as the page's base URL. If null defaults to 'about:blank'.
data a String of data in the given encoding
mimeType the MIMEType of the data, e.g. 'text/html'. If null, defaults to 'text/html'.
encoding the encoding of the data
historyUrl the URL to use as the history entry. If null defaults to 'about:blank'.
You can try passing the assets folder as the baseUrl, so my guess would be that your code would be like this
Hope this helps!

Android WebView not loading local html string correctly

I'm working on a project where I'm receiving some html string which I should show in WebView. The problem is that the html string contains only body part, without any tags like <html>, <head>, <body> and when I'm trying to show the received string in webView the result is showing the tags included in html string received from server.
Here is some example what I receive and what I'm doing :
Html String received from server :
05-30 14:02:56.785: D/(16970): body : <p align='justify'>The so-called Fiscal Compact is unlikely to be approved by the Latvian parliament in the second and final reading as four opposition MPs said that they would not back the initiative, local media reported on Tuesday evening. The second reading is scheduled for May 31. Under local legislation, the bill should be backed by 67 MPs in the 100-seat parliament, though the ruling coalition controls only 56 seats and depends on the opposition Union of Greens and Farmers (ZZS) to approve it. During the first reading 68 MPs backed the bill. The four MPs from ZZS will meet PM Dombrovskis later today to discuss the issue. They want more clarity about the country’s participation in neighboring Lithuania’s nuclear power plant project. Previously, ZZS said it extends its support on condition that the government fights for higher subsidies for farmers in the 2014-2020 EU budget framework.</p>
Here is my code :
String bodyTxt = cursor.getString(cursor.getColumnIndex("body"));
Log.d("","body : "+bodyTxt);
String newBody = "<html><body>" + bodyTxt + "</body></html>";
body.loadDataWithBaseURL(null, newBody, "text/html", "utf-8", null);
and the result is this :
I've tried using body.loadData(newBody, "text/html", "UTF-8");
, but this way didn't even show me the result.
So any ideas which I'm doing wrong and how can I fix this?
Thanks in advance!
As I can see from your logs you are receiving the html tags not with < symbol,but like >. I guess if you replace these characters in your html string it will work as it should be. Try this :
String bodyTxt = cursor.getString(cursor.getColumnIndex("body"));
String replaced = bodyTxt.replace("<","<");
Log.d("","replaced : "+replaced);
String replaced2 = replaced.replace(">", ">");
Log.d("","replaced2 : "+replaced2);
Log.d("","body : "+bodyTxt);
String newBody = "<html><body>" + replaced2 + "</body></html>";
body.loadDataWithBaseURL(null, newBody, "text/html", "utf-8", null);
you can use this
webview.loadData(Html.fromHtml(htmlData),"text/html","utf-8");
This is how i do it;
String htmlContent = "<html><body>hello</body></html>";
WebView wv = (WebView) findViewById(R.id.my_view);
wv.loadData(htmlContent, "text/html", "UTF-8");
Investigate how the data looks in the db, all the "<>" and such are quoted with "<", this might be the problem you looking for.

Does webview take HTML tag <object>?

I tried many times and try to use HTML tag <object>, but app just crash and disappear.
Each time when I use normal html tag such as <html>, <body>, <h1>, <div>, <td>, <tr> etc. Those can be shown directly without any error.
This time I try to embed an <object> tag to webview and it always crash!
Does anyone who know how to do this directly inside code? OR, webview just cannot do this?
here is my code.
String html =
"<html>" +
"<body><h1>This is a test !!</h1>" +
"<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='200' height='200'></object>" +
"<param name='quality' value='high'>" +
"<param name='allownetworking' value='internal'>" +
"<param name='allowScriptAccess' value='never'>" +
"<param name='movie' value='http://video.yutube.com/flv2.swf?i=20100129LqvamMPB&d=360.25&movie_stop=off&no_progressive=1&otag=1&sj=5&rel=1'>" +
"<param name='allowFullScreen' value='true'>" +
"<embed src='http://video.yutube.com/flv2.swf?i=20100129LqvamMPB&d=360.25&movie_stop=off&no_progressive=1&otag=1&sj=5&rel=1' width='200' height='200' quality='high' allownetworking='internal' allowscriptaccess='never' allowfullscreen='true' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed>" +
"</object>" +
"</body></html>";
mWebView.loadDataWithBaseURL("", html, "text/html", "utf-8", "");
You have one <object> tag and two </object> tags. The first </object> has got to go.
The embed tag is for browsers that don't recognize the object tag. Those browsers will see the embed tag and will try to use it. Browsers that support the object tag will use the object and ignore the embed, as long as the embed is within the object. If the embed is outside the object, object aware browsers may load both the object and embed tags.
WebView can definitely handle the html tag. I don't think you should use loadDataWithBaseURL though, try this instead:
mWebView.loadData(html, "text/html", "UTF-8");
I'm not too sure your <object> will work though...
Here's a better way to play a YouTube video if that's all you need:
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v=<video-id>")));
Put your video id in place of the <video-id>

Categories

Resources