Getting blank page in Android WebViewClient - android

I am trying open twitter link : http://mobile.twitter.com/pawan_rathore88
in my activity. If I set WebViewClient to webview I am getting blank page.
But when I load url without setting any webviewclient, it loads page properly.
Does anyone have idea what can be a problem. Following is my code snippet.
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
//if I comment the following line then webpage loads properly in default Android browser.
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.v(tag, "url :" + url);
view.loadUrl(url);
return true;
}
});
webview.loadUrl("http://mobile.twitter.com/pawan_rathore88");
Thanks,
Pawan

After tweaking the code around, it seems to be a user agent problem, seems that changing it to a desktop useragent fixes this problem :
WebView web = (WebView)findViewById(R.id.webView1);
web.getSettings().setUserAgentString("Mozilla/5.0 (Macintosh; " +
"U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
"like Gecko) Version/5.0 Safari/533.16");
String url = "http://mobile.twitter.com/pawan_rathore88";
web.loadUrl(url);

This is a problem with twitter at the moment and fails in all web kit mobile browsers.

That is exact my problem too. Load Desktop version very good but not mobile version. If running on default browser, then Mobile version normally run. I think the problem is not well perform user string agent. But cannot find it out now.

Related

Android WebView Not Loading Correctly

I'm having a few issues regarding the WebViewClient on Android.
The site works perfectly on any mobile browser. Including the ChromeViewClient that I have set for debugging purposes.
And the website that I am loading does not have any issues or errors when using any other mobile browser. Using Chrome's inspector and selecting a device, using as mentioned the native Android browser and also tested on an iOS WebView Component to make sure.
The WebViewClient renders "parts" of the website. Images on one page and not the other, buttons that can not be clicked, a slider that does not work, etc. The website that I am loading is very JavaScript and HTML5 intensive. I am completely out of ideas of how to debug this issue further, are there certain JavaScript libraries that the WebViewClient can't load properly? Is there any other method you would recommend I implement while trying to debug this issue? Or am I missing some really small thing that will make me hit my head against the table?
These are the JS files we are using on the website:
bootstrap.min.js;
jquery.min.js;
swiper.jquery.min.js;
slideout.min.js;
owl.carousel.min.js.
Code for the WebView:
this.webview = (WebView)findViewById(R.id.webView);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
firstLoad = true;
return true;
}
// when the page is finished loading
public void onPageFinished(WebView view, String url){}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
Toast.makeText(getBaseContext(), "Could Not load. " + description, Toast.LENGTH_SHORT).show();
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
return;
}
});
alertDialog.show();
}
});
webview.loadUrl("mywebsite.com");
I got it working by setDomStorageEnabled(true);
You need to set this when using local storage.

Images not loading in android webview

I am trying to load the site in an android app web view.
The site loads without the images ,all the images from the site are not loaded what could be the problem.
The code for onCreate is shown below.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
String url = getResources().getString(R.string.web_url);
web = (WebView) findViewById(R.id.webview01);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setUseWideViewPort(true);
web.loadUrl(url);
}
One more note, when i set javascript to false using web.getSettings().setJavaScriptEnabled(false); the images load giving a warning that javascript should be enabled to run the site properly.
This site is protected with CloudFlare ,could this be the reason images are not loaded in the android web view ?
Add
web.getSettings().setDomStorageEnabled(true);
This should work. Keep the rest as is.
Although a caveat, I don't think its recommended to enable this by default, its disabled by default. The setting allows a website to store data and reuse it. So the images are not being loaded, because this site wasn't allowed to store them on your device. This opens up a can of security concerns.
Just use this line of code. I think your problem will be solved.
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
You may need this one as well, if you are lazy-loading content:
webView.getSettings().setJavaScriptEnabled(true);
Just this will suffice
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
I had similar problem in Android 9.0. The images in the site's html were using http instead of https.
Then I changed all the http with https and everything worked!
It was very easy to change the http to https using a sql query in mySql.
I am giving the query if it helps anyone!
UPDATE table_name SET column_name = replace(column_name, '<img src="http://', '<img src="https://')
I suspect that the issue might be your webview client.
Use this instead:
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (pDialog.isShowing()) {
pDialog.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
Toast.makeText(FundCardWeb.this, "Page Load Error" + description, Toast.LENGTH_SHORT).show();
}
});
for me the combination of adding those did it:
webSettings.setDomStorageEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setLoadsImagesAutomatically(true);
I don't like adding unneccesery cod when so I didn't use:
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
I have faced the same issue. In the web view, the Image with the http URL was not loading in my app. The following solution fixed my issue,
webview.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
But in Android 9, the above solution has thrown the issue sometimes, in such case add the following line in the manifest file.
android:usesCleartextTraffic="true"
My problem was just one image, not loading in webview. I just add this two thing
In Manifest
android:usesCleartextTraffic="true"
In java oncreate() just add
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mywebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}

Webview doesn't show a specific website

I'm trying to do a webview based application for this website to show it in a mobile application.
when I put any different site the application work great, but in this specific site never show the second page when I clicked in the button to go to the desk page. However, I put a Log statement in the onPageFinished method and log that the page is loaded completely.
My Code Here
final WebView myWebView = (WebView) rootView.findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getActivity(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
#Override
public void onPageFinished(WebView view, String url) {
Log.d("WEBSITE", "Page Loaded.");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("WEBSITE", url);
myWebView.loadUrl(url);
return false;
}
});
myWebView.loadUrl("https://demo.frappecloud.com/");
I believe the problem is with your shouldOverrideUrlLoading. If you check the documentation you will see that :
This method is not called for requests using the POST "method"
When you are submitting the Form, you are making a POST request, which is basically ignored.
Here is the link: WebViewClient
Also check this reference where it says how you could load a URL with POST data: LOAD A POST REQUEST INTO A WEBVIEW IN ANDROID
Consider checking this thread: Android - how to intercept a form POST

Android WebView loading part of a website

Some websites have comments portion and many other unnecessary portions. I want to just display the main article excluding the other portions in my custom Android WebView.
I have tried the example from Display a part of the webpage on the webview android
But it still shows the full website.
There is also the loaddatawithbaseurl class in Android but it is not working too as you can't specify till which portion of a website to show using this class.
Is there any other way to do this?
Here it is with some changes...
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url)
{
String javaScript = "javascript: var form = document.getElementsByClassName('university_corner');"
+ "var body = document.getElementsByTagName('div');"
+ "body[0].innerHTML = form[0].innerHTML";
webView.loadUrl(javaScript);
}
});
webView.loadUrl(url);

How To Link To An Android App From Android Webview

Currently I have an Android app that is basically a web view loading a web page. On the web page I've tryed to link to the market like this...
https://market.android.com/details?id=com.google.earth
http://market.android.com/details?id=com.google.earth
market://details?id=com.google.earth
The first result just opens up a white screen (It may be loading, but it has been their for over a minute).
The second result says that the page has been moved and a link. If you click th link it does what the first one did.
The third result says that the page may be temporarily down. (It's treating the link like its online rather than in the phone itself)
Here is how the link looks...
echo "<a href='https://market.android.com/details?id=com.google.earth' data-role='button'>Upgrade Now</a>";
Remember the web page I'm loading is using JQuery Mobile and I'm echoing the link with php.
How can I open a link to the Android Market in a webview on a web page?
In my case, I was getting the same blank/moved temporarily trouble described. I wanted to use the shouldOverrideUrlLoading so that the native browser wasn't used in an oauth2 flow from my page to google and back to my page. My android app was talking to localhost/tomcat with a self-signed cert. Turned out I needed to call proceed because of cert mismatch:
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("DevAgentSocialMain", "URL: " + url);
view.loadUrl(url);
return true;
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.i("DevAgentSocialMain", "onReceivedError: " + errorCode + " " + description + " url: " + failingUrl);
super.onReceivedError(view, errorCode, description, failingUrl);
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
Log.i("DevAgentSocialMain", "onReceivedSslError: " + error.getPrimaryError());
//super.onReceivedSslError(view, handler, error);
handler.proceed();
}
});
You can use a callback when a user clicks a link inside the webview.
See
Handling Page Navigationsection on the android developer platform.
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(MyWebViewClient);
Then your callback
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}

Categories

Resources