I have a webView where url is: https://uesr:user#test.move.com:443/test-mobile and when I want to load this page it is unaviable, where I delete authentication from page and delete user:user# page work. How add authentication to webview to work with that url?
this is my webView:
formWebView.getSettings().setJavaScriptEnabled(true);
formWebView.loadUrl(url);
formWebView.getSettings().setBuiltInZoomControls(true);
I resolve my problem by:
formWebView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler,
String host,
String realm){
handler.proceed(loggedUser.getLogin(), loggedUser.getPass());
}
public void onReceivedSslError(WebView view,
SslErrorHandler handler,
SslError error) {
handler.proceed() ;
}
});
Write below code in your onCreate method.
webView.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient
{
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm) {
handler.proceed("UserName", "Password");
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (dialog != null) {
if (!dialog.isShowing()) {
dialog.show();
}
}
}
public void onLoadResource(WebView view, String url) {
// TODO Auto-generated method stub
super.onLoadResource(view, url);
}
public void onScaleChanged(WebView view, float oldScale, float newScale) {
// TODO Auto-generated method stub
super.onScaleChanged(view, oldScale, newScale);
}
public void onTooManyRedirects(WebView view, Message cancelMsg,
Message continueMsg) {
// TODO Auto-generated method stub
super.onTooManyRedirects(view, cancelMsg, continueMsg);
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
return super.shouldOverrideUrlLoading(view, url);
}
public void doUpdateVisitedHistory(WebView view, String url,
boolean isReload) {
// TODO Auto-generated method stub
super.doUpdateVisitedHistory(view, url, isReload);
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
public void onPageFinished(WebView view, String url) {
if (dialog != null) {
dialog.dismiss();
}
}
}
Related
If i return true on shouldOverrideUrlLoading, web view cannot handle onPageFinish when load new url.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.abc.com")) {
Intent intent = new Intent(context, MyActivity.class);
context.startActivity(intent);
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
Disable progress on page finish.
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progress.setVisibility(View.GONE);
}
try this code
webview.setWebViewClient(new myWebClient());
webview.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view, int progressInt)
{
if (progressInt < 80 && progress.getVisibility() == ProgressBar.GONE)
{
progress.setVisibility(ProgressBar.VISIBLE);
}
if (progressInt >= 80)
{
progress.setVisibility(ProgressBar.GONE);
}
}
});
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
progress.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progress.setVisibility(View.GONE);
}
}
Few urls are not loading in the webview. Please check the code. If i load google it loads properly. Any Solution? URL LOADS PROPERLY IN THE BROWSER.
Here this url (and few others also) is not loading wv.loadUrl("http://resident.uidai.net.in/get-enroled");
Instead if google is loaded it loads properly. How can i forcefully load this url?
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar1);
wv = (WebView) rootView.findViewById(R.id.wvHome);
wv.getSettings().setSupportZoom(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
wv.setScrollbarFadingEnabled(true);
wv.getSettings().setLoadsImagesAutomatically(true);
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
wv.setWebViewClient(new WebViewClient());
try {
wv.loadUrl("http://resident.uidai.net.in/get-enroled");
//wv.loadUrl("http://eaadhaar.uidai.gov.in");
}
catch (Exception e)
{
wv.loadUrl("http://google.com");
}
// Inflate the layout for this fragment
return rootView;
}
private class HelloWebViewClient extends WebViewClient{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url)
{
webView.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(view.GONE);
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // Ignore SSL certificate errors
}
}
1st. change http to https
2nd. Change wv.setWebViewClient(new HelloWebViewClient());
3rd. override the following
private class HelloWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
webView.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(view.GONE);
}
#SuppressLint("NewApi")
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // Ignore SSL certificate errors
}
}
I am working with xWalkWebView
and I would like to know the equivalent to this code
webView.setWebViewClient(new WebViewClient() {
#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);
}
#Override
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm) {
}
});
Thanks using Crosswalk, you can also ask question by sending email to
crosswalk-help#lists.crosswalk-project.org
For this question:
mXWalkView.setUIClient(new XWalkUIClient(mXWalkView) {
#Override
public void onPageLoadStarted(XWalkView view, String url) {
System.out.println("onPageLoadStarted " +url);
}
#Override
public void onPageLoadStopped(XWalkView view, String url,
LoadStatus status) {
System.out.println("onPageLoadStopped " + status);
}
});
mXWalkView.setResourceClient(new XWalkResourceClient(mXWalkView) {
#Override
public void onReceivedHttpAuthRequest(XWalkView view,
XWalkHttpAuthHandler handler, String host, String realm) {
System.out.println("onReceivedHttpAuthRequest");
}
});
I am making a web view and trying to show loading message using progress Dialog.progress dialog is showing but not dismissed after all the content of URL is loaded.Please Help me.
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
ProgressDialog prDialog;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
prDialog = ProgressDialog.show(Web.this, null, "loading, please wait...");
super.onPageStarted(mWebview, url, favicon);
Toast.makeText(getApplicationContext(), "started!"+1, Toast.LENGTH_SHORT).show();
num++;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(mWebview, url);
prDialog.dismiss();
Toast.makeText(getApplicationContext(), "Done!", Toast.LENGTH_SHORT).show();
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://www.google.com");
setContentView(mWebview);
package com.example.webviewtag;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewDemo extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
webView.setClickable(true);
webView.setFocusableInTouchMode(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
WebClientClass webViewClient = new WebClientClass();
webView.setWebViewClient(webViewClient);
WebChromeClient webChromeClient=new WebChromeClient();
webView.setWebChromeClient(webChromeClient);
setContentView(webView);
}
public class WebClientClass extends WebViewClient {
ProgressDialog pd = null;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
pd = new ProgressDialog(WebViewDemo.this);
pd.setTitle("Please wait");
pd.setMessage("Page is loading..");
pd.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
pd.dismiss();
}
}
public class WebChromeClass extends WebChromeClient{
}
}
Try this code,
private class MYWEBCLIENT extends WebViewClient {
private ProgressDialog prDialog;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
prDialog = ProgressDialog.show(activity, "", "Please wait...");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (prDialog != null && prDialog.isShowing())
prDialog.dismiss();
}
}
Load webview code,
webViewInfo.getSettings().setJavaScriptEnabled(true);
webViewInfo.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webViewInfo.setWebViewClient(new MYWEBCLIENT());
webViewInfo.loadData("YOUR_URL_OR_HTML_FILE", "text/html", "UTF-8");
You can try this java code:
Just need to replace my entities with yours. Such as class name or other variables or stuff.
public class WebViewDemo extends Activity
{
WebView mWebview;
ProgressDialog prDialog;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
mWebview = (WebView) findViewById(R.id.your_id);
mWebview.getSettings().setJavaScriptEnabled(true);
CustomWebClient webClient =new CustomWebClient(WebViewDemo.this);
mWebview.setWebViewClient(webClient);
mWebview .loadUrl("http://www.google.com");
}
// This is your custom webviewclient
public class CustomWebClient extends WebViewClient
{
public Context context;
public CustomWebClient(Context context)
{
// TODO Auto-generated constructor stub
this.context = context;
prDialog = new ProgressDialog(context);
prDialog.setMessage("loading please wait...");
prDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
prDialog.show();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url)
{
// TODO Auto-generated method stub
super.onPageFinished(view, url);
try
{
if ((prDialog != null) && prDialog.isShowing())
{
prDialog.dismiss();
}
} catch (final IllegalArgumentException ae) {
} catch (final Exception excep) {
} finally {
prDialog = null;
}
}
#Override
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm)
{
// TODO Auto-generated method stub
super.onReceivedHttpAuthRequest(view, handler, host, realm);
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl)
{
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
}
You can run my java class to test, is it working or not at your end.
Hope this helps you.
Android.webview.WebViewClient class lets activities receive an WebViewClient.onReceivedError callback when the WebView is unable to load a URL, but there is no callback for HTTP Errors, like: 401, 403, 404, 500. How can we handle this error in WebView?
Here is some code:
public class WebViewActivity extends Activity {
WebView webView;
public String TAG="log";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webview1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://192.168.0.6:8081/karbonn/add/apk//3_1/");
// webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
String pageTitle = view.getTitle();
Toast.makeText(getBaseContext(), pageTitle, Toast.LENGTH_SHORT).show();
// view.get
/* if (pageTitle.contains("Apache Tomcat/7.0.40 - Error report")) {
// view.goBack();
WebViewActivity.this.finish();
//view.loadUrl("https://www.google.co.in/");
}*/
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
//Log.e(Tag, "onReceivedError");
//int i=errorCode;
if (errorCode == ERROR_HOST_LOOKUP || errorCode == ERROR_FILE_NOT_FOUND) {
// TODO your dialog here
Log.e("error", "ERROR_FILE_NOT_FOUND"+errorCode);
}
Log.e("error", "onReceivedError"+errorCode);
if (errorCode==-10) {
WebViewActivity.this.finish();
}
String errormsg=description;
String url=failingUrl;
// TODO Auto-generated method stub
Log.e("error code:" ,""+errorCode);
super.onReceivedError(view, errorCode, description, failingUrl);
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public WebResourceResponse shouldInterceptRequest(WebView view,
String url) {
// TODO Auto-generated method stub
Log.e("WebResourceResponse" ,""+url);
return super.shouldInterceptRequest(view, url);
}
#Override
public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
// TODO Auto-generated method stub
return super.shouldOverrideKeyEvent(view, event);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
String myurl=url;
Log.e("url", ""+myurl);
return super.shouldOverrideUrlLoading(view, url);
}
});
//webView.loadUrl("http://trax.claym.in/click.php?c=209&key=c73n783g2tm2377yd73d6n5m");
//webView.loadUrl("https://www.google.co.in/");
//animation url
// webView.loadUrl("http://www.dogwonder.co.uk/wp-content/uploads/2009/12/tumblr_ku2pvuJkJG1qz9qooo1_r1_400.gif");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
// TODO Auto-generated method stub
return super.onKeyDown(keyCode, event);
}
}
Sorry but you can't.
There you have the bug and thread where the devs are telling so.
https://code.google.com/p/android/issues/detail?id=968
Just you can do on the new Android M (API 23) with the new method
public void onReceivedHttpError (WebView view, WebResourceRequest request, WebResourceResponse errorResponse)
Ref: https://developer.android.com/intl/es/reference/android/webkit/WebViewClient.html#onReceivedHttpError%28android.webkit.WebView,%20android.webkit.WebResourceRequest,%20android.webkit.WebResourceResponse%29
Hope this helps