Webview sometimes shows webpage not available - android

On all of my apps I am getting a "webpage not available" error. This has just been happening recently, even on apps that have not been updated in awhile. At first I thought it was my server or domain name. However, everything loads on a mobile browser or desktop browser. The strangest part is that I can click on the link for the website given by the error and it works. Also, this isn't every time I get into the app. It sometimes load without any problems at all.
This is my main activity:
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Allow third party cookies for Android Lollipop
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mWebView = (WebView) findViewById(R.id.activity_main_webview);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptThirdPartyCookies(mWebView,true);
}
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.getSettings().setDatabaseEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.loadUrl("http://www.google.com");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && this.mWebView.canGoBack()) {
this.mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}

You instantiated WebView twice that could be the reason.
cookieManager.setAcceptThirdPartyCookies(mWebView,true);
You don't need this one below, you've done it already.
mWebView = (WebView)findViewById(R.id.activity_main_webview);

Related

WebView is exiting instead of going back

As an example, here used google as the website. When we click on any link and try to go back by hitting back button, it not calling goBack() but exiting. Back button in websites also not working.
public class MainActivity extends Activity {
public WebView mWebView;
#SuppressLint({"setJavaScriptEnabled"})
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = findViewById(R.id.WebView);
mWebView.clearCache(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
mWebView.loadUrl("http://google.com");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
I'm using this code for months without any error, suddenly i got issue this few days back without any modifications. I also tried by calling goBack() in onBackPressed() with if statement but still shows same issue. Any help will be appreciated!!
because its chrome bug : https://bugs.chromium.org/p/chromium/issues/detail?id=794020
This has been fixed in M64. You can find a public release calendar here [1]. This is just an approximate schedule, but we aim to be close to the public schedule.
According to the schedule, M64 will go to stable January 23rd.
While M64 is currently in beta, we have not yet made a beta release with the fix. I do not have information for when such a beta release will go out (but it will be sooner than Jan 23rd).
[1] https://www.chromium.org/developers/calendar

HTTP-EQUIV="REFRESH" isn't working on android web view

I have a web page with a basic form (like a newsletter subscription).
The user sends its information and gets a success page with a link to the form submission page. If he doesn't click on the link the page will be automatically redirected after 5 seconds.
<meta http-equiv="refresh" content="5;url=www.site.com" />
So far so good! It works as expected in both desktop and mobile browsers.
Then I created an android app (min API SDK 21) with a web view to load the site.
The problem is that the auto refresh isn't working inside the web view...
Am I missing something? I'm trying to avoid the javascript hack...
Thanks for your time!
EDIT
I notice that when I click on the new form submission page link it opens the default browser instead of rendering it on the web view!
After searching for the problem, I came to a solution by resetting the URL on URL loading.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mWebView = findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
mWebView.loadUrl(url);
return true;
}
});
mWebView.loadUrl(url);
}
It's working, but I think it can be improved.
My initial problem was that I wasn't setting a WebViewClient.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mWebView = findViewById(R.id.webview);
mWebView.loadUrl(url);
}
Thus, I think I just need to add a WebViewClient and it'll work
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mWebView = findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl(url);
}
Note, and if we want javascript to work we need to enable it
mWebView.getSettings().setJavaScriptEnabled(true);

webview won't remember facebook login

In my android app the user can like my facebook page. Therefore I use a webview that loads a webpage that contains a facebook like box. The page is loaded well into the webview and then the following happens:
WebView loads my webpage with a facebook like box
By clicking on the Like button the user is redirected to the facebook login page
After login the user is redirect again back to my custom like page
But when clicking the like button the user is again redirected to the facebook login page
So I would expect, that the like is possible after logging in. It seems somehow as if the webview does not remember the login. Therefore, what do I have to do to repair this.
The following screenshot sequence shows what's happening:
I want to avoid using the facebook sdk for android! Especially this procedure works very good in my iphone version of the app.
Here is some code that is used to implement my desired functionality:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView fbWebView = (WebView) findViewById( R.id.facebookWebView );
fbWebView.getSettings().setJavaScriptEnabled(true);
fbWebView.setWebViewClient( new WebViewClient() {
#Override
public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
Log.d("call url", url );
if ( url.contains("login.php?skip_api_login") ) {
fbWebView.loadUrl("http://www.example.de/iphone_facebook_like_page.html");
return null;
}
if( url.contains("user_successfully_liked") )
fbWebView.loadUrl("http://www.example-success.de");
return null;
}
});
fbWebView.loadUrl("http://www.example.de/iphone_facebook_like_page.html");
}
EDIT: I also tried the following to accept cookies but none of this worked
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().startSync();
CookieManager.setAcceptFileSchemeCookies(true);
CookieManager.getInstance().setAcceptCookie(true);
//rest is the same...
Overrding didn't work either
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
You have that error, because WebView isn't remembering cookies. Read that question on SO : WebView and Cookies on Android
Not sure if you've gotten an answer to your problem, but I tried a combination of these instructions and the code below, and that worked for me:
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
...
WebView webView = new WebView();
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAppCacheEnabled(true);
settings.setDomStorageEnabled(true);
settings.setGeolocationEnabled(true); // this last one is probably optional
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(<your url here>);
...
}
You will then need to call the corresponding methods for the CookieSyncManager singleton class as outlined in the link above. Note that the sync class is deprecated in API Level 21: see details on CookieSyncManager class

Android - when exiting a WebView, the audio still keeps playing

I have this code which works and the only problem with it is that if I leave the WebView and even close the app, and even press power on the phone, the audio from the podcasts keeps playing.
Would anyone know how to stop that?
Here is the code:
public class PodcastsActivity extends BaseActivity //implements ActionBar.OnNavigationListener
{
WebView webview = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
//setTheme(R.style.Theme_Sherlock_Light);
super.onCreate(savedInstanceState);
//setContentView(R.layout.podcasts);
webview = new WebView(this);
webview.getSettings().setAppCacheEnabled(false);
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(1);
webview.getSettings().setPluginState(PluginState.ON);
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
//webSettings.setBuiltInZoomControls(true);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
//webSettings.getMediaPlaybackRequiresUserGesture();
webSettings.setAllowContentAccess(true);
webSettings.setEnableSmoothTransition(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setSupportZoom(true);
webSettings.setUseWideViewPort(true);
setContentView(webview);
webview.loadUrl("http://www.stitcher.com/podcast/entrepreneuronfirecom/entrepreneur-on-fire-tim-ferriss-other-incredible-entrepreneurs");
}
// public void onBackPressed ( )
// {
// Class.forName("com.***.HTML5WebView").getMethod("onPause", (Class[]) null).
// invoke(html5WebView, (Object[]) null);
// }
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack())
{
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onStop()
{
super.onStop();
// your code
webview.clearView();
}
You should call through to the WebView's onPause() and onResume() from your Activity's onPause() and onResume(), respectively.
Pauses any extra processing associated with this WebView and its
associated DOM, plugins, JavaScript etc. For example, if this WebView
is taken offscreen, this could be called to reduce unnecessary CPU or
network traffic. When this WebView is again "active", call onResume().
There's also pauseTimers(), which affects all of the WebViews within your application:
Pauses all layout, parsing, and JavaScript timers for all WebViews.
This is a global requests, not restricted to just this WebView. This
can be useful if the application has been paused.
Accepted answer didnt work for me - just using onPause in a fragment was not enough to stop music on Android 9. I had to also do:
override fun onDestroyView() {
webview?.destroy()
super.onDestroyView()
}
Same issue for the Jetpack Compose can be solved by capturing the WebView (https://google.github.io/accompanist/webview/) in "onCreated" and then use it to call "destroy()"
val webViewState = rememberWebViewState(webViewUrl.value)
val webView = remember { mutableStateOf<android.webkit.WebView?>(null) }
//..inside Composable..
WebView(state = webViewState,
captureBackPresses = true,
onCreated = { wv ->
webView.value = wv
}
)
//..on Back Press/Close Button..
webView.value?.destroy()
In my case WebView onPause() and onResume() is not working for me.
Solution:
onPause I load the below URL to stop the audio:
webView.loadUrl("javascript:document.location=document.location");

Ability to play YouTube videos in webview?

I want to use flash to play a YouTube video in a webview.
For example, when the WebView loads youtube.com and you click on a video, it will play it fully without any limitations. Currently I get a grey video with a broken flash player picture.
I've tried to add:
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginState( WebSettings.PluginState.ON );
I've also added android:hardwareAccelerated="true" to the Manifest XML without any luck.
I'm running this on my Nexus 7, don't know if this makes a difference. I've checked every nook and cranny of the internet and haven't found a solution.
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new ClientOP());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginState( WebSettings.PluginState.ON );
mWebView.loadUrl("http://www.youtube.com");
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
private class ClientOP extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println("URL: " + url);
view.loadUrl(url);
return true;
}
}
Android doesn't support flash anymore. Why don't you use the new Youtube api?
https://developers.google.com/youtube/android/player/

Categories

Resources