Android Webview url not loading - android

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
}
}

Related

Loading process getting delay when load any page in android webview

I am using android in that I am calling a link via webview. Now, the problem is that whenever I click any button or call any page of that link at that time after some second progress bar coming i.e. progress bar performance is delay while load anything.
From this problem user can't know that button/link has been clicked or not, without getting any progress bar sign, and hence from that user clicked many times that button/link.
Here is my android code of MainActivity.java,
public class MainActivity extends AppCompatActivity {
WebView webView;
ProgressBar bar;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar = (ProgressBar)findViewById(R.id.progressBar2);
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setAppCacheEnabled(true);
webView.setWebViewClient(new myWebClient());
webView.loadUrl("http://florro-001-site2.etempurl.com/");
}
public class myWebClient extends WebViewClient{
#Override
public void onPageFinished(WebView view, String url) {
bar.setVisibility(View.GONE);
view.setVisibility(webView.VISIBLE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
bar.setVisibility(View.VISIBLE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean overrideUrlLoading = false;
view.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()){
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
I searched many things but related topics I didn't get.
use bar.setVisibility(View.VISIBLE); in the shouldOverrideUrlLoading not in onPageStarted try to use like that
public class myWebClient extends WebViewClient{
#Override
public void onPageFinished(WebView view, String url) {
bar.setVisibility(View.GONE);
view.setVisibility(webView.VISIBLE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean overrideUrlLoading = false;
view.loadUrl(url);
bar.setVisibility(View.VISIBLE);
return true;
}
}

android - Sometimes get '502 Bad Gateway' in WebView

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);
}
};

Android loginvia facebook in pinterest is not working in webview

Android - Am loading a following url in webview "https://www.pinterest.com", it loads successfully, but when i click on login via facebook or login via google in pinterest it's loading a blank screen rather than loading the actual result, below is my code, please help me. The same code is working for other urls, but not for Pinterest alone.
public class Pinterest extends Fragment {
private WebView wv1;
String url="https://www.pinterest.com";
private ProgressBar progress;
View mView;
WebSettings settings;
ImageView back,top;
public Pinterest() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mView= inflater.inflate(R.layout.pinterest, container, false);
wv1 = mView.findViewById(R.id.pin_webView);
progress = mView.findViewById(R.id.pin_progressBar);
back= mView.findViewById(R.id.go_back);
top= mView.findViewById(R.id.goToTop);
progress.setMax(100);
settings = wv1.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowContentAccess(true);
settings.setDomStorageEnabled(true);
wv1.setWebViewClient(new myWebClient());
wv1.setVerticalScrollBarEnabled(true);
wv1.setHorizontalScrollBarEnabled(true);
wv1.loadUrl(url);
progress.setProgress(0);
wv1.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
setValue(newProgress);
if (newProgress == 100) {
progress.setVisibility(View.GONE);
}
super.onProgressChanged(view, newProgress);
}
});
back.setOnClickListener(v -> {
if(wv1.canGoBack())wv1.goBack();
});
top.setOnClickListener(v -> {
Constants.goTop(wv1);
});
return mView;
}
public void setValue(int progress) {
this.progress.setProgress(progress);
}
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
System.out.println("when you click on any interlink on webview that
time you got url :-" + url);
// view.loadUrl(url);
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
System.out.println("your current url when webpage loading.. finish"
+ url);
super.onPageFinished(view, url);
}
}
}
It looks like requests from WebView are working only when the Facebook app is installed on the device.
See Practice #5 in the Facebook docs.
https://developers.facebook.com/docs/facebook-login/best-practices?locale=en_US
Google has been blocking since April 2017 all the requests related OAuth2, which are using WebView (due to security concerns).

Progress bar load on every link click

How to make ProgressBar load/appear on every click on link in webview?
currently after click, page is blank for nearly 5 to 7 seconds then ProgessBar appears...
Probably you can use WebViewClient (see WebView.setWebViewClient(WebViewClient client)) and override its method
onPageStarted(WebView view, String url, Bitmap favicon)
to start showing your ProgressBar...
E.g.like this:
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
Best practice for doing this is using WebViewClient.
EXAMPLE
WebView webView;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_layout);
webView = (WebView) findViewById(R.id.webView_blog);
progressBar = (ProgressBar) findViewById(R.id.loading);
lala_icon = (ImageView) findViewById(R.id.lala_icon);
webView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
progressBar.setVisibility(View.VISIBLE);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.e("url", url);
progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.e("Loading URL", url);
progressBar.setVisibility(View.VISIBLE);
}
public void onPageFinished(WebView view, String url) {
try {
if (progressBar.getVisibility() == View.VISIBLE) {
progressBar.setVisibility(View.GONE);
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
});
}
Happy Coding.

ProgressDialog is not dismiss

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.

Categories

Resources