I am trying to load the cached version of the website when mobile internet is off. But I am only getting a white screen.
Here is my code.
binding.webView.getSettings().setJavaScriptEnabled(true);
binding.webView.getSettings().setDomStorageEnabled(true);
binding.webView.getSettings().setAppCacheEnabled(true);
binding.webView.getSettings().setAllowFileAccess(true);
binding.webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
binding.webView.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath());
if (!isNetworkAvailable()) {
binding.webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);
}
binding.webView.setWebChromeClient(new WebChromeClient());
binding.webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
binding.progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
binding.progressBar.setVisibility(View.GONE);
binding.webView.setVisibility(View.VISIBLE);
}
});
binding.webView.loadUrl("website url here");
Try changing this line:
binding.webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
To:
binding.webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
Related
I got '502 Bad Gateway' from WebViewClient' onReceivedHttpError, and then there is just one message: "the specified cgi application encountered an error and the server terminated the process." showing in WebView without any content.
I got this issue after re-opened the WebView many times, but the link work fine on PC browser. When I re-install the application, it shown normal content in the first few times.
I tried to change WebSettings below, but still not work.
#SuppressLint("SetJavaScriptEnabled")
private void settingWebView() {
final WebSettings settings = webView.getSettings();
// js
settings.setJavaScriptEnabled(true);
// ui control
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
settings.setDisplayZoomControls(false);
// cache
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
// others
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
// web view
webView.setHorizontalScrollBarEnabled(false);
webView.setInitialScale(100);
// client
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
super.onReceivedHttpError(view, request, errorResponse);
Log.e(TAG, "onReceivedHttpError: " + errorResponse.getStatusCode());
Log.e(TAG, "onReceivedHttpError: " + errorResponse.getReasonPhrase());
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
});
webView.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
return true;
}
});
webView.loadUrl(DevelopUtil.TACTICS_WEB_VIEW);
}
Do anybody knows what is the problem ?
Is it server error or my WebView setting error ?
try this it worked for me
WebViewClient webClient = new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onFormResubmission(WebView view, Message dontResend,
Message resend) {
// TODO Auto-generated method stub
super.onFormResubmission(view, dontResend, resend);
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
if (url.contains("purchasehistory.html")) {
// mURLNavigation.onURLNavigation(3);
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
};
Trying to load this website with WebView, but it just show a blank page.
I can open other website with no problem, but not the above website. Possible the web developer block it? The website is not owned by me.
guideView = (WebView) findViewById(R.id.guideView);
guideView.setWebViewClient(new myWebClient());
guideView.getSettings().setJavaScriptEnabled(true);
guideView.loadUrl("https://sapsnkra.moe.gov.my/ibubapa2/index.php");
}
public class myWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
#Override
public void onPageStarted(WebView view, String url,
Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
I tried to setWebChromeClient, but it do not work as well.
This link is apparently is blocked to access. I tried to access and dont work.
Make sure this .php is working to network access. As it is a .gov url there may be blocks.
This is the code to my WebView. It works great on every device I have tested so far, even a Samsung S4. However, a certain Samsung S4, which the client insists it doesn't have a modified software, displays a white screen every time the app is resumed from background. Any ideas?
private void setupWebView() {
// hide the no connection imageview
// show the webview
noConnectionImageView.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedSslError(WebView view,
SslErrorHandler handler, SslError error) {
handler.proceed();
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
showNoConnectionImage();
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// page has started loading
if (mustShowSplashScreen) {
showSplashScreen();
}
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// page has finished loading
if (mustShowSplashScreen) {
hideSplashScreen();
mustShowSplashScreen = false;
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// check for Internet connection
if (ConnectionDetector.isConnected(Main.this)) {
// check if link is external
if (isExternalUrl(url) {
// external link
new OpenLink(Main.this, url);
} else {
// internal link
view.loadUrl(url);
}
} else {
showNoConnectionImage();
Main.this.url = url;
}
return true;
}
});
}
We have a requirement where we want multiple webview instance to be open from same application.
E.g Android app1 should open page1.html in first webview instance and page2.html in second webview instance. When page2.html is started page1.html webview should go in background.
Is this possible in Android? Is yes, Can you please provide sample code or link which talks in details as how this requirement can be achieved.
public void callwebtwo(){
loadweb = (WebView) findViewById(R.id.loadweb);
loadweb2 = (WebView) findViewById(R.id.loadweb2);
loadweb2.getSettings().setJavaScriptEnabled(true);
loadweb.getSettings().setJavaScriptEnabled(true);
loadweb2.loadUrl("http://www.google.co.in/");
loadweb.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("shouldOverrideUrlLoading", url.toString());
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
Log.i("onPageFinished", url.toString());
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i("onPageStarted", url.toString());
super.onPageStarted(view, url, favicon);
}
}
});
loadweb2.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("shouldOverrideUrlLoading", url.toString());
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
Log.i("onPageFinished", url.toString());
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i("onPageStarted", url.toString());
super.onPageStarted(view, url, favicon);
}
});
}
instance of the both web is different so it will work and perform seperatly..
i gvn ans whtever i cn understand frm ur quest.
Yes you can do it in android.
Create seperate activities for both and when you load page2 in second webview instance then obvisouly page1 webview instance will be in back of second and when you press back from page2 then page1 will be infront.
I'm looking to develop an application to track how long a site takes to load on the default android browser. Does the browser have any method of tracking when the site is fully loaded?
on page started and on page finished are the methods to
get the the start time and end time of loading of the webview
WebView search_webview;
search_webview = (WebView) findViewById(R.id.show_webview);
search_webview.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
_dialog.dismiss();
super.onPageFinished(view, url);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
try{
_dialog.dismiss();
Toast.makeText(ShowWebView.this, "NO Internet Connection Available", Toast.LENGTH_SHORT);
}catch (Exception e) {
// TODO: handle exception
}
}
});