Limit in Loading content into WebView - android

i have a problem for load content into my Web View.
The problem is:
i have string like this:
String temp=myobj.get(0).getstring; //very long string
webview.loadDataWithBaseURL("", temp, "text/html; charset=utf-8", "utf-8", "");
and when i get the result of
myobj.get(0).getstring; //very long string
using Log, and set it manually, every thing was OK.
so i really don't know what is the problem.
do any one know what is the reason of the problem?
EDIT: in fact i test a couple of more thing, and i understand it show string but don't show images.

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 clear html page before showing into a webview in Android?

I have the URL of a webpage to be displayed into a webview in my Android app. Before showing this page i want to clear the html code of this page from some tag (such as the header, footer, ecc..) in order to show only few information. How can i do it? I tried to solve the issue working with JSoup but i can't understand how to create and pass the "new page" to the webview. Anybody can help me?
EDIT
I cleaned the html code useless through jsoup libraries. Then, always by mean of these, i get head and body content and finally i showing the "cleared" web page through these lines:
headURL = doc.select("head").outerHtml();
bodyURL = doc.select("body").outerHtml();
webview.loadData( "<html>"+headURL+bodyURL+"</html>" , "text/html", "charset=UTF-8");
webview.setWebViewClient(new DisPlayWebPageActivityClient());
The view shows the new page but do not load css files specified in the head(that has not been touched). Who can say me why?
You can fetch the WebPage you want to display as a string, parse and remove whatever you don't want and then load this string as data in your webview.
Something like:
String webContent = fetchPage(url);
String cleanedWebContent = cleanUp(webContent);
webView.loadData(cleanedWebContent, "text/html", "UTF-8");
Of course, you will need to implement fetchPage and cleanUp as they are not Android methods

Android - Webview truncates string

I have a Webview where I am trying to show this string:
"---#%-#%%#:-=%#+=%-####%#%--.....-=.-=:-:.........=----::::::::=---:::::::::::::.............................................::::.........................................-#+ ."
However, the string is truncated and displayed in more than one line, and I can't figure out why. It should be displayed in only one line. Any idea on how to solve this issue?
This is how I am populating the webview:
web.loadDataWithBaseURL(null, s, "text/html", "utf-8", null);
where s is the string previously showed.
Thanks in advance

Had to load data twice to make WebView refresh in Android

When I first create the activity, everything goes fine. However, after I choose from menu to change some text of the String values and set the webview by
webview.loadData(result, "text/html; charset=UTF-8", null);
webview.loadData(result, "text/html; charset=UTF-8", null);
I have to do it twice, or the webview will keep unchanged. Is there anyone knows what happens here? Since the result String is the same, why webview force me to loadData twice?
Avoid WebView#loadData(String data, String mimeType, String encoding) - it's buggy.
Use WebView#loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl) instead.
So your instruction will be like:
webview.loadDataWithBaseURL(null,result,"text/html", "utf-8", null);
Don't know what's your problem but looking at the webview documentation, you are using the loadData method wrongly :
Webview:loadData documentation
You probably should call your webview like this :
webview.loadData(result, "text/html", "UTF-8");
Don't know if it will solve your issue at all.
Yes with loadDataWithBaseURL it does refresh the data, but then it ignores the CSS body background-color! ... At least it can't parse "%23000000" which works with loadData.
I am loading local HTML data into my webview, and this webview is inside recyclerview,
When I try webview.loadData() when it renders 1st time it working fine, but when I scrolling upward downward every inflated webview`s get messed-up.
When I try second webview.loadDataWithBaseURL() its working like charm.
so,when you're loading the HTML locally and it references assets such as images & css which are also packaged locally use webview.loadDataWithBaseURL()

Android - Search for text inside of a Webview after loading

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.

Categories

Resources