WebView show nothing when load some URL on Android - android

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>

Related

PDF No Preview Available in android webview

I am developing an android app in which I have to load a PDF in a webview by appending http://drive.google.com/viewerng/viewer?embedded=true&url= before the actual URL. But after loading it is not previewing the document and saying that "No Preview Available". But the same document is loaded in iOS webview and the Desktop chrome. At the same time, other PDF files from internet are loading properly. I dont know whether it is the issue with the file or my code. Here is my code
`
public class WebViewActivity extends AppCompatActivity {
private WebView webView;
private String postUrl;
private String doc_type;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
this.webView = (WebView) findViewById(R.id.webView);
this.webView.getSettings().setJavaScriptEnabled(true);
this.webView.setHorizontalScrollBarEnabled(false);
this.webView.getSettings().setAllowFileAccess(true);
doc_type = getIntent().getStringExtra("doc_type");
String url = getIntent().getStringExtra("click_action_url");
if (doc_type != null && doc_type.equalsIgnoreCase("others")) {
postUrl = url;
} else{
postUrl = "http://drive.google.com/viewerng/viewer?embedded=true&url=" + url;
}
this.webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
}
});
this.webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#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);
}
});
this.webView.loadUrl(postUrl);
}
}`
Someone please help.
I had the same issue, try encoding the url before loading it. I had answered here https://stackoverflow.com/a/67245546/1843984

WebView shows net::ERR_CLEARTEXT_NOT_PERMITTED on HTTPS url

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

android webview: how to intercept form-data when redirect?

I have questions. and sorry my pool english skill.
In android webivew,
I load some page. and this page is redirect another page.
when redirect, url is contain some data(post, form-data).
I want catch contain data. befor loaded url.
I think, override onPageStarted method and catch data. but cannot.
how can I?
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
mWebView.postUrl(URL, postdata.getBytes("UTF-8")); // Sample Page
} catch (UnsupportedEncodingException e) {
Logger.log(TAG, e.getMessage());
}
}
private class WebViewClientClass extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
//catch redirect form-data.
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
}
}
Please add below line then only it will use your custom class.
webView.setWebViewClient(new WebViewClientClass());
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//write your code here, get redirected url in this method and do your operation and then move ahead.
return false;
}
Above method will always called when redirect url is getting loaded.
Hope this will help you.

web view url goes to browser .how to do avoid and open in same web view?

here my code...am already implemented WebViewClient concept.it works well in web view..In my App have content saring via fb,twitter,g+,,,fb also login and open in webview...but when am click share to fb icon in my app after that all process will work on browser..but i want all process in webview only...
oncreate
webView = (WebView) findViewById(R.id.webView1);
webView.setClickable(true);
webView.setFocusableInTouchMode(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new myWebClient());
if (Build.VERSION.SDK_INT < 8) {
...
} else {
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
}
webView.loadUrl("www.example.com.");
WebViewClient
public class myWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
public void onLoadResource (WebView view, String url) {
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/myerrorpage.html");
}
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) {
super.onPageFinished(view, url);
progress.setVisibility(View.GONE);
}
}
You must override your shouldOverrideUrlLoading() method in WebViewClient class and following code
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
Go through the documentation for more information.
Use a if clause in your shouldOverrideUrlLoading method that returns false;
Example from Building web apps in web views
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}

how to get the current page url from the web view in android

Using webview.loadUrl(url) method I open an url. If I click any Button on the view it directs to another page. Now I want to get the url of the directed page. how can I get it?
I also want the content that is displayed on the webview. How to get the content of the WebView ?
String webUrl = webView.getUrl();
please see my answer
may it will help you...!!!!
WebView webview = new WebView(context);
webview.setWebViewClient(new WebViewClient()
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.d("WebView", "your current url when webpage loading.." + url);
}
#Override
public void onPageFinished(WebView view, String url) {
Log.d("WebView", "your current url when webpage loading.. finish" + url);
super.onPageFinished(view, url);
}
#Override
public void onLoadResource(WebView view, String url) {
// TODO Auto-generated method stub
super.onLoadResource(view, url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println("when you click on any interlink on webview that time you got url :-" + url);
return super.shouldOverrideUrlLoading(view, url);
}
});
I am assuming that you have set your WebViewClient.If not then you can do like below,
webView.setWebViewClient(new MyWebViewClient());
String currentUrl;
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
currentUrl=url;
return true;
}
}
Here when you click on any link on the WebView then it will call shouldOverrideUrlLoading() and you can get the current url there in currentUrl.
this is the most simple way of handling this for API > 20: There are obviously other methods with parsers like HTMLCleaner to have more control. However, this is good and simple for getting URL. Each time URL changes in Webview, URL in the request object changes as well.
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String temp = request.getUrl().toString();
if(temp.isEmpty()){
Log.i(ActivityName, "No url returned!");
}else{
Log.i(ActivityName, temp);
}
return false;
}
you can also make your call by overriding onpagefinished method of webviewclient. I wanted to note this, since this is called when page finishes loading.

Categories

Resources