I am trying to load a url using webview in android. This is how my webview looks like
public class WebViewFragment extends Fragment {
private static final String LOG_TAG = WebViewFragment.class.getName();
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.web_view_fragment, container, false);
init(rootView);
return rootView;
}
private void init(View rootView) {
Bundle bundle = getArguments();
ImageView backButtonIV = (ImageView) rootView.findViewById(R.id.backButtonImage);
backButtonIV.setOnClickListener(new PopBackStackClickListener(getActivity()));
String title = bundle.getString(BundleConstants.WEB_HEADER_TEXT);
TextView headerTextTV = (TextView) rootView.findViewById(R.id.pageTitle);
headerTextTV.setText(title);
WebView webView = (WebView) rootView.findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.setWebViewClient(new MyWebView());
String urlToLoad = bundle.getString(BundleConstants.WEB_URL);
Log.d(LOG_TAG, "Opening url :" + urlToLoad);
webView.loadUrl(urlToLoad);
}
private class MyWebView extends WebViewClient {
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
#Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
return super.shouldInterceptRequest(view, request);
}
#Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
super.onReceivedHttpError(view, request, errorResponse);
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
}
}
I put a breakpoint on onReceivedHttpEror and found that there were two "not found" errors. Basically there are two .png files that is not found. The webview for this reason stops loading the page. How can I ignore these errors and proceed? Because outside of android, if I try to open the link the web page loads fine and the errors are just logged in console. But it doesn't hamper the loading of the page. How can I achieve the same in android webview and load the page successfully?
Try to replace the code in your overrides of onReceivedError and onReceivedHttpError with simply logging (do not call super).
Try this, if you donĀ“t call super it should work:
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
Log.d(LOG_TAG, "onReceivedError :" + error);
}
#Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
Log.d(LOG_TAG, "onReceivedHttpError:" + errorResponse);
}
Related
I'm trying to load an url that fit security protocols with HTTPS, but when I'm trying to load on a WebView, android shows me net::ERR_CLEARTEXT_NOT_PERMITTED. Why? is a HTTPS what is the problem?
The source code that shows it is:
public class InternalWebBrowserActivityHelperImpl implements InternalWebBrowserActivityHelper, Constants {
private final String TAG = getClass().getSimpleName();
#NonNull
private InternalWebBrowserActivityView activityView;
public InternalWebBrowserActivityHelperImpl(#NonNull InternalWebBrowserActivityView activityView){
this.activityView = activityView;
}
public WebChromeClient getWebChromeClient = new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
}
};
public WebViewClient getWebViewClient() {
return new WebViewClient() {
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
LoggerManager.handlesError("onReceivedError", request.getUrl().toString());
}else{
LoggerManager.handlesError("onReceivedError", error.toString());
}
activityView.hideLoadingView();
activityView.showWebView();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
LoggerManager.handlesError("onPageFinished", url);
activityView.hideLoadingView();
activityView.showWebView();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//activityView.showLoadingView();
LoggerManager.handlesError("override", url);
return super.shouldOverrideUrlLoading(view, url);
}
};
}
#Override
public void setupHelper(String url) {
//7activityView.showLoadingView();
activityView.showWebView();
WebSettings.ZoomDensity zoomDensity = WebSettings.ZoomDensity.FAR;
activityView.getFullWebView().getSettings().setJavaScriptEnabled(true);
activityView.getFullWebView().getSettings().setDomStorageEnabled(true); // Add this
activityView.getFullWebView().getSettings().setDefaultZoom(zoomDensity);
activityView.getFullWebView().getSettings().setSupportZoom(true);
activityView.getFullWebView().getSettings().setBuiltInZoomControls(true);
activityView.getFullWebView().requestFocus(View.FOCUS_DOWN);
activityView.getFullWebView().setWebChromeClient(getWebChromeClient);
activityView.getFullWebView().setWebViewClient(getWebViewClient());
activityView.getFullWebView().loadUrl(url);
}
}
Thanks
Are you sure the URL you give to your webview is in HTTPS ?
Otherwise a quickfix would be to add the below line in your application as shown below:
<application
android:usesCleartextTraffic="true"
android:networkSecurityConfig="#xml/network_security_config"
....
</application>
I strongly advise that you create the network_security_config to only allow your domain and subdomain. Here is a quick tutorial
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();
}
I have a demo with WebView on Android. Here is my code:
public class AuthPortalActivity extends Activity {
WebView authPortalWebview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.auth_portal_activity);
authPortalWebview = (WebView) findViewById(R.id.auth_portal_webview);
authPortalWebview.setWebViewClient(new WebViewPortal());
openProvisioningPortal();
}
private void openProvisioningPortal() {
authPortalWebview.getSettings().setLoadsImagesAutomatically(true);
authPortalWebview.getSettings().setJavaScriptEnabled(true);
authPortalWebview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
authPortalWebview.loadUrl("https://example.com");
}
}
class WebViewPortal extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("TAG", "shouldOverrideUrlLoading url = " + url);
return true;
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
#Override
public void onPageFinished(final WebView view, String url) {
super.onPageFinished(view, url);
Log.d("TAG", "onPageFinished");
}
}
It can load some URL like Google, Facebook... But when I try with my Portal URL, it can not show anything. Sure my Portal using "https" and it also load successfully if I use browser like Chrome...
Here is my WebView after load URL and method onPageFinished had been called.
Did I forgot something in my code ??
either return false in shouldOverrideUrlLoading to proceed loading the url normally
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("TAG", "shouldOverrideUrlLoading url = " + url);
return false;
// ^^^^^
}
or
You have your own client WebViewPortal so you need to load the url as well
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("TAG", "shouldOverrideUrlLoading url = " + url);
view.loadUrl(url);
//^^^^^^^^^^^^^^ load the url once you receive web view and link
return true;
}
WebViewClient is responsible to process the request and proceed according to the notifications onPageFinish or started etc.
use this line in Androidmanifest.xml
//-------------this is Internet permission-------------------
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
I have my activity that loads webview as below
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set layout
setContentView(R.layout.activity_webview);
// get loading view
loadingView = findViewById(R.id.loadingView);
loadingView.setVisibility(View.VISIBLE);
// extract extras from intent
Intent intent = getIntent();
// get and setup WebView
webview = (WebView) findViewById(R.id.webView);
// implement progress bar
webview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
loadingView.setVisibility(View.GONE);
// if no title was passed in with the intent use the page's title
if (TextUtils.isEmpty(title)) {
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle(view.getTitle());
}
}
#SuppressWarnings("deprecation")
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
showErrorAndFinish();
}
#TargetApi(Build.VERSION_CODES.M)
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
// Redirect to deprecated method, so you can use it in all SDK versions
onReceivedError(view, error.getErrorCode(), error.getDescription().toString(), request.getUrl().toString());
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
return super.shouldInterceptRequest(view, request);
}
});
// load the url
webView.loadUrl(url);
}
However, I need to look at response of the Url that I load and call to another API URL based on the response.
I have looked so far for thisURL Loading QUestion but not much helpful under 5.0. Any suggestions on how should I approach? Should I just use Retrofit in shouldOverrideUrlLoading() method?
Also, I cannot modify both the URL's that I am loading .
I would appreciate the response
I have a webview in my app, however sometimes due to connectivity the webview fails to load and I get the default webpage unavailable page. I want to show an alertdialog if the webview failed to load. Is there anyway I can check (maybe in the shouldOverridePageLoad function) that a webview loaded successfully? Thanks again
Use a WebClient on your web view as follow :
webView.setWebViewClient(new WebViewClient(){
#Override public void onReceivedError(WebView view, WebResourceRequest request,
WebResourceError error) {
super.onReceivedError(view, request, error);
// Do something
}
});
Extending on Damien's answer on using WebViewClient, there are four listeners available on WebViewClient to check for the success and failure of loading web pages.
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
}
#Override
public void onReceivedHttpError(
WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler,
SslError error) {
}
});
There is also:
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
}
});
which is deprecated in favor of its overload mentioned in the above code.