I am trying to display a captcha jpeg image (which is generated dynamically by a Java servlet) in my application's WebView, however, it will sometimes display a blue question mark instead of the captcha image. Below is what I have in my shouldOverrideUrlLoading(WebView view, String url) method of MyWebViewClient class that extends WebViewClient
I have internet access enabled in my manifest and the problem occurs for about 80% of the time. i.e. Sometimes the image does show up correctly but very rare though.
There was another similar question here Image with dynamic src loads in Android browser, but not in Webview, but does not seem to have a definitive answer.
Thanks in advance
try
{
CookieSyncManager.getInstance().sync();
Log.v("Hello","CookieStore: " + httpClient.getCookieStore().toString());
HttpResponse response = httpClient.execute(httppost);
data = new BasicResponseHandler().handleResponse(response);
view.loadDataWithBaseURL(URL.toString(), data, "text/html", "UTF-8" , null);
//Log.v("Hello","Data: " + data.toString());
}
catch (ClientProtocolException e)
{
Log.v("Hello","ClientProtocolException:Overriding");
Log.v("Hello",e.toString());
e.printStackTrace();
}
catch (IOException e)
{
Log.v("Hello","IOException:Overriding");
Log.v("Hello",e.toString());
e.printStackTrace();
}
I just got the same problem. I identified the issue and solved it (in my case). You can notice that in HTML code, the img tag has the src pointing to the relative path of the site. Something like <img src="/relativepath/captcha.jpg?12345678"/>. All you have to do is to inject the full path (host + relative path) to obtain something like <img src="http://yourhost.com/relativepath/captcha.jpg?12345678">.
Read the HTML code, line by line
Match the sequence "<img src" (maybe something a little bit differente in your site) with each line
When you found the line you will split it until you find the link inside the src=""
Reconstruct your tag with the host name on it: <img src="http://host.com" + link >
Then the image will appear ;)
Try using view.loadUrl(URL.toString()); instead of loadDataWithBaseURL
Related
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.
I need to load some HTML that I construct at runtime into a WebView and apply a CSS file to it placed in the assets directory. And I already have a base URL that I need to provide to the webview's loadDataWithBaseURL function.
Code Snippet (not applying CSS file):
StringBuffer buff = new StringBuffer();
buff.append("<head>");
buff.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"file:///android_asset/my_css_file.css\"/>");
buff.append("</head>");
buff.append("<div class=\"announcement-header\">");
buff.append("HEADER");
buff.append("</div>");
buff.append("<div class=\"announcement-separator\"></div>");
buff.append("<div class=\"announcement\">");
buff.append("CONTENT");
buff.append("</div>")
WebView.loadDataWithBaseURL(MY_BASE_URL, buff.toString(), "text/html", HTTP.UTF_8, null);
I've looked at these 2 similar issues Issue 1 & Issue 2, but my case is slightly different as I cant give file:///android_asset/ as the base url to the loadDataWithBaseURL function.
Any ideas how I can apply the CSS file in this case ?
If you want to intercept some of the requests from WebView you can do so by overriding shouldInterceptRequest() in WebViewClient like this:
public WebResourceResponse shouldInterceptRequest (final WebView view, String url) {
if (URLUtil.isFileUrl(url) && url.contains(".css")) {
return getCssWebResourceResponseFromAsset();
} else {
return super.shouldInterceptRequest(view, url);
}
}
There is already an excellent detailed answer here https://stackoverflow.com/a/8274881/1112882
HOWEVER
You can not access local file if your base url is not local because of security reasons. So give relative path to your css and then intercept the request.
I would suggest using a pattern to differentiate b/w actual relative and custom relative paths. e.g. use android_asset/my_css_file.css instead of my_css_file.css and modify shouldInterceptRequest() accordingly to always intercept requests starting from android_asset.
I'm using a WebView to display a html page which is stored in the \raw\ directory so I can easily localize it.
I load the page using
InputStream inputStream = getResources().openRawResource(R.id.htmlPage1);
StringWriter writer = new StringWriter();
try {
IOUtils.copy(inputStream, writer);
} catch (IOException e) {
e.printStackTrace();
}
String x = writer.toString();
WebView w = (WebView) myFragmentView.findViewById(R.id.webView);
w.loadDataWithBaseURL("file:///android_asset/", x, "text/html", "utf-8", null);
which works fine. Images and css files used in the html page are stored in the \assets\ directory.
Now I want to add links to another local page (< a href="htmlPage2.html">Click here< /a>) in my html page. The WebView tries to load the page from assets (which seems to be the correct behavior). Unfortunately the \assets\ folder isn't localized.
How can I link to other local html pages without giving up the feature of localization?
Thanks!
Step #1: Probably use a more distinctive URL pattern for the links to help you distinguish them (e.g., <a href="/this/is/another/raw/resource/htmlPage2.html">)
Step #2: Register a WebViewClient with your WebView
Step #3: In shouldOverrideUrlLoading() of your WebViewClient, if the URL matches the pattern you use for these links (see Step #1), go through your load-the-resource logic and return true, else do nothing and return false to pop up a Web browser on that URL
I've been searching, but I can't find a code that works with mine, I've found some similar questions, but the others not work with my "after loading" code.
What I need is search for a text, if exist show the page normally (do nothing, I mean, let continue), if not exist, show a html message on the same webview with this code:
String MsgError = "<html><body><h1 style='color:#FF0000'>Error</h1><br /><a style='color:#000000'>An error occurred while page is loading, please try again later.</a></body></html>";
mWebView.loadData(MsgError, "text/html", "UTF-8");
I have to find a text like this one:
Text
This is my "after loading" code:
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.clearCache(true);
}
Thanks in advanced.
Perhaps it may be interesting to look here, where somebody explains how to extract the HTML as a string from a webview. You could perform a String.matches() on that string. Depending on the result you get, you can either let the WebView be, or redirect it to your own, custom error page.
If I push this HTML into WebView:
webView.loadData("<html><body><pre>line 1\nline 2</pre></body></html>", "text/html", "utf-8");
it renders as (in emulator and also on device)
line 1line 2
as opposed to
line 1
line 2
as I would expect. If I save this HTML to the sdcard and open the file in the browser, it renders fine. I suppose I am doing something wrong, or this may be a bug. Any way, I want to programatically push HTML with preformatted newlines into a WebView and have the newlines rendered.
The string passed to loadData needs to be URI-escaped.
You can use URLEncoder.encode() to do that, but for some reason WebView does not decode the '+' back to a ' '. One work around is to replace all the '+' with '%20' yourself.
For example (and with the '+' translation):
try {
webview.loadData(URLEncoder.encode("<html><body><pre>line 1\nline 2</pre></body></html>", "utf-8").replaceAll("\\+", "%20"), "text/html", "utf-8");
} catch (UnsupportedEncodingException uee) {
Log.e("webview", "", uee);
}
Try this:
webView.loadDataWithBaseURL(...)
More info here
Also you can use
chapterWebView.loadDataWithBaseURL("file:///android_asset/NTImages/", message.replaceAll("\\n", "<br/>") , "text/html", "utf-8", "utf-8");