I want to load a webpage.
private class MyJavaScriptInterface {
private MyJavaScriptInterface () {
}
public void setHtml(String contentHtml) {
if (contentHtml != null && contentHtml.trim().length() > 0) {
//Do something
}
}
}
private WebViewClient webViewClient = new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:window.ResponseChecker.setHtml"
+ "(document.body.innerHTML);");
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
public void onReceivedSslError(WebView view, SslErrorHandler handler,
SslError error) {
handler.proceed();
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e("ProcessPayment", "onReceivedError = " + errorCode);
}
};
I want to handle webpage loading errors. I know that the errors can be obtained in onReceivedError(...) method.
My problem is how can I handle the error without showing Page Not found in webview? (eg: Show a dialog and makes webview blank).
Thanks in Advance.
Check as:
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Log.e("ProcessPayment", "onReceivedError = " + errorCode);
//404 : error code for Page Not found
if(errorCode==404){
// show Alert here for Page Not found
view.loadUrl("file:///android_asset/Page_Not_found.html");
}
else{
}
}
public void onReceivedError(WebView view, int errorCode, String description,
String failingUrl) {
if (errorCode == ERROR_HOST_LOOKUP || errorCode == ERROR_FILE_NOT_FOUND) {
// TODO your dialog here
}
}
Related
i am trying load a .php file in WebView. it was working properly before API level 28 after i updated to API level 28 its not working its showing white screen. nothing is showing i tried all the options.
Here is the code
I am added the safe browsing false in the manifest file also.
I tried searching in the google nothing is helped me if any one done please help in this case to resolve.
String url="https://xxx/xxx/abc.php";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handleSSLHandshake();
WebView mWebView = (WebView) findViewById(R.id.webView);
// PackageInfo webViewPackageInfo = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// webViewPackageInfo = WebView.getCurrentWebViewPackage();
mWebView.getSettings().setSafeBrowsingEnabled(false);
// Log.d("MY_APP_TAG", "WebView version: " + webViewPackageInfo.versionName);
}
WebViewClientImpl webViewClient = new WebViewClientImpl(this);
mWebView.setWebViewClient(webViewClient);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(url);
if (18 < Build.VERSION.SDK_INT ){
//18 = JellyBean MR2, KITKAT=19
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
}
}
public class WebViewClientImpl extends WebViewClient {
private Activity activity = null;
public WebViewClientImpl(Activity activity) {
this.activity = activity;
}
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
webView.loadUrl(url);
// Log.i(TAG,url);
return true;
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
}
}
I solved my problem. it was the certificates issues, i just added the following code, its working fine now
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
This question already exists:
How to save form's info last time in webview
Closed 5 years ago.
1.Webview has a goback method , it can save web state when click back.
2.My layout is a Linearlayout and it has a webview on top of two button.
Every button match a url. Also I add webView.goBack() in onKeyDown method
The first url has a form, the second doesn't have a form
I wrote some info at form in first url, after I click the second button
then ,I click the first button,but the url is reload.but if when I click back
the form info is save.
I want to save those form info like when I click back button,it can save last form info
I want to save form info in last time, how to do that?
my code is here
private void checkone(String url) {
if (!Netutils.isNetworkAvalible(MyApplication.mcontext)) {
fl.removeAllViews();
webView.loadUrl(url);
webView.setVisibility(View.GONE);
webView.removeAllViews();
fl.setVisibility(View.VISIBLE);
fl.addView(mErrorView);
} else {
handler.postDelayed(new Runnable() {
#Override
public void run() {
fl.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
fl.removeAllViews();
}
},1000);
Map extraHeaders = new HashMap();
extraHeaders.put("Referer", "www.emiaoqian.com");
webView.loadUrl(url, extraHeaders);
settings = webView.getSettings();
settings.setBuiltInZoomControls(true);
settings.setUseWideViewPort(true);
settings.setJavaScriptEnabled(true);
settings.setSaveFormData(true);
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
settings.setAppCachePath(MyApplication.mcontext.getCacheDir().getPath());
webView.setWebChromeClient(new ChomeClient(this) {
#Override
public void onProgressChanged(WebView view, int newProgress) {
currentProgress = pg1.getProgress();
if (newProgress >= 100 && !isAnimStart) {
isAnimStart = true;
pg1.setProgress(newProgress);
startDismissAnimation(pg1.getProgress());
} else {
startProgressAnimation(newProgress);
}
}
#Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
if (title.contains("404")) {
showErrorPage();
}
}
});
settings.setAllowFileAccess(true);
settings.setAllowContentAccess(true);
settings.setDomStorageEnabled(true);
String ua = webView.getSettings().getUserAgentString();
webView.getSettings().setUserAgentString(ua.replace("Android", "emiaoqian"));
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
pg1.setVisibility(View.VISIBLE);
pg1.setAlpha(1.0f);
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("weixin://wap/pay?")) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
//startActivityForResult(intent,233);
startActivity(intent);
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
CookieSyncManager.getInstance().sync();
} else {
CookieManager.getInstance().flush();
}
if (!(url.equals(Constants.LOGIN)) && !(url.equals(Constants.LOGINOUT))) {
if (homeTb != null && supportActionBar != null) {
supportActionBar.setDisplayHomeAsUpEnabled(false);
supportActionBar.setDisplayShowHomeEnabled(false);
homeTb.setTitle("");
}
radiogroup.setVisibility(View.VISIBLE);
nouserl.setVisibility(View.VISIBLE);
} else if (url.equals(Constants.LOGIN) || url.equals(Constants.LOGINOUT)) {
inittoolbar2();
radiogroup.setVisibility(View.GONE);
nouserl.setVisibility(View.GONE);
}
if (url.startsWith("https://wx.tenpay.com")) {
String newurl = url.substring(url.length() - 12, url.length());
LogUtil.e("--", newurl);
}
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
showErrorPage();
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
showErrorPage();
}
});
fixDirPath();
// webParentView = (LinearLayout) webView.getParent();
}
}
You can set :
mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); // WebSettings.LOAD_CACHE_ELSE_NETWORK
Also to Save in Specify the app cache path :
mWebView.getSettings().setAppCachePath(mContext.getCacheDir().getPath());
And tell me the result.
public class Site extends Activity {
WebView mWebview;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.site);
mWebview = (WebView) findViewById(R.id.webview);
mWebview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
Intent IntentGM= getIntent();
Bundle b = IntentGM.getExtras();
String GMget =(String) b.get("GameMania");
String MGget =(String) b.get("MediaGames");
String BCget =(String) b.get("BolCom");
String webUrl = mWebview.getUrl();
final Activity Site = this;
mWebview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
Site.setProgress(progress * 1000);
}
});
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(Site, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
if (GMget != null && GMget.length() > 4)
{
String Link = "http://www.gamemania.nl";
mWebview.setWebViewClient(new WebViewClient()
{
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl(Link);
setContentView(mWebview );
}
else if (MGget != null && MGget.length() > 4)
{
String Link = "http://www.mediamarkt.nl/games";
mWebview.setWebViewClient(new WebViewClient()
{
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl(Link);
setContentView(mWebview );
}
else if (BCget != null && BCget.length() > 4)
{
String Link = "http://www.bol.com/games";
mWebview.setWebViewClient(new WebViewClient()
{
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl(Link);
setContentView(mWebview );
}
}
}
I think it's my if-else query, but i don't know what to change to get it working.
I get this error when i want to open this activity from a button whereof i have to go to Bol.com inside this webview.
What i want to have is an activity with full screen webview, but also buttons for url-sharing, go back & other options above the webview-screen.
I got error code if any error in webpage by onReceivedHttpError method of setWebViewClient but when there is no error any page load success or The server callback me code such as 403 then how can i check if status code 200 or 403.
You could use in web view
webView.setWebViewClient(new WebViewClient() {
#SuppressWarnings("deprecation")
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// Handle the error
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
#Override
public void onReceivedSslError(final WebView view, final SslErrorHandler handler, final SslError error) {
// TODO Auto-generated method stub
super.onReceivedSslError(view, handler, error);
// handle your SSL related error here
// handler.proceed();
}
});
I have some web page which open in WebView.
<body onload="window.location.href='htcmd:loaded';">
After load we open back url "htcmd:loaded" and intercept in code.
Like this:
getWebView().getSettings().setJavaScriptEnabled(true);
getWebView().setWebViewClient(new WebViewClient() {
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if ("htcmd:loaded".equals(url)) {
Toast.makeText(getActivity(), "htcmd:loaded", Toast.LENGTH_SHORT).show();
}
return true;
}
});
getWebView().loadUrl("https://some.url");
On android 4.4.2 in first start all is well. But if I kill app and open after first run, web page not render. But if I tap on screen or change orientation web page appears. Where is problem?
SOLUTION: I have two hacks)))
First: add a java script to web page:
<body onload="setTimeout(function(){window.location.href='htcmd:loaded';},3000);">
Second: add code to web client:
#Override
public void onPageFinished(WebView view, String url) {
if (android.os.Build.VERSION.SDK_INT >= 19) {
view.requestFocus();
}
}
try this
w.getSettings().setLoadWithOverviewMode(true);
w.getSettings().setUseWideViewPort(true);
getWebView().getSettings().setJavaScriptEnabled(true);
getWebView().setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
});
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
getWebView().loadUrl("https://www.google.com");