Android WebView shows "Resource not found" - android

I Want to open a WeView inside my App. It works fine for few pages navigation, then suddenly it shows "Resource not found". Here is my code. Am I missing something here.
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://xyz.example.com");
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});

Return false instead of true in shouldOverrideUrlLoading()
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://xyz.example.com");
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});

Related

Web View not working with some specific url

public class MainActivity extends AppCompatActivity {
String url = "https://www.pinterest.com";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
}
});
webView.loadUrl(url);
}
}
Web View is not loading "https://www.pinterest.com" within the application but it was working with chrome browser using intent. I tried of other urls it's working. While loading "https://www.pinterest.com" using webview, getting the output screen like below
You try this:
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowContentAccess(true);
settings.setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.pinterest.com/");
In manifest:
android:hardwareAccelerated="true"
set the JavascriptEnabled and AllowContentAccess and setDomStorageEnabled to true in settings

i have a webview app and facing that when i click on Hyperlink then it open in default browse

I have a webview app and facing a problem that
when I click on Hyperlink then it opens in default browser.
But I want to open that link in same webview.
How to open it in same webview.
Here is my code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.myWebView);
myWebView.loadUrl("file:///android_asset/abc/index.html");
myWebView.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("file:///android_asset/abc/index.html")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Do it like,
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Return false in all cases.
ok.. you have added a WebViewClient also..
then try it in this way...
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
this should work for you..

Link in WebChromeClient does not open safari when using WebViewClient shouldOverrideUrlLoading

I use WebChromeClient for opening a link to news.html on my server, it opens safari and shows the content, ok.
But...
When I implent WebViewClient shouldOverrideUrlLoading to intercept the call when it's link to a .pdf file (use another class for it) the WebChromeClient link to news.html stays in webview and does not open safari anymore...
I am doing something wrong, but what?
Code snippet: (links are long so I shortened it)
myWebView = (WebView)findViewById(R.id.webView);
myWebView.getSettings().setJavaScriptEnabled(true);
//only to catch url override
myWebView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("http://www.domain.nl/pdf")){
Intent i = new Intent();
i.putExtra("url", url);
i.setClassName("nl.domain.domain", "nl.domain.domain.PdfActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
return true ;
}
else {
return false ;
}
}
});
myWebView.setWebChromeClient(new WebChromeClient());
//loading
myWebView.loadUrl("http://www.domain.nl/news.html");
return false; means that the url wasn't handled and it should be opened in the WebView. If you want to open the url in an external browser you need to explicitly do it.
myWebView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("http://www.domain.nl/pdf")){
Intent i = new Intent();
i.putExtra("url", url);
i.setClassName("nl.domain.domain", "nl.domain.domain.PdfActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
return true ;
}
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url));
return true;
}
});
To open the PDF file url :
add the "http://docs.google.com/viewerembedded=true&url=" string at starting of PDF file url string...
try this it work
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView;
webView = new WebView(this);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setPluginsEnabled(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
if (webUrl.equals(XmlFileUrl.MemberDownloadAppForm)) {
webUrl = "http://docs.google.com/viewerembedded=true&url=" + webUrl;
}
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
setContentView(webView);
}
An example that helped me to do something similar Here
For geolocation add:
myWebView.getSettings().setGeolocationEnabled(true);
and :
private class MyWebChromeClient extends WebChromeClient {
.........
#Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback)
{
// callback.invoke(String origin, boolean allow, boolean remember);
callback.invoke(origin, true, false);
}
}

WebView and mailto link

Problem resolved, I forgot to add this line:
myWebView.getSettings().setJavaScriptEnabled(true);
I havew a WebView with a email link.
Here is my HTML email link:
href="javascript:location.href='mailto:'+String.fromCharCode(97,110,105,109,97,108,115,64,109,100,112,105,46,99,111,109)+'?'">animals#mdpi.com</a>.</p>
I am trying to catch when the user clicks on this link with this code:
myWebView.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView wv, String url) {
if(url.contains("mailto")){
System.out.println("mailto");
}
return true;
}
});
But I am never entering in the if.
Here is the solution
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView wv, String url) {
if(url.contains("mailto")){
System.out.println("mailto");
}
return true;
}
});

WebView with shouldOverrideUrlLoading and baseURL

I have a WebView on my application and some links which are URL adresses.
Some of them don't have the baseURL, only something like "/abc/abc"
I would like to set the baseURL for all the links starting with "/".
I have tryed this:
WebView wv = (WebView) findViewById(R.id.EditorialWebView);
wv.setBackgroundColor(0x00000000);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadDataWithBaseURL("", editorialBoard, "text/html", "utf-8", "");
wv.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView wv, String url) {
if (url.startsWith("/"))
{
wv.loadUrl("http://www.mdpi.com"+url);
}
return true;
}
});
But nothing happens.
Try something like this
myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("/")) {
Uri u = Uri.fromParts("http://www.domain.com", url, "");
view.loadUrl(u);
}
return true;
}
});
Please let me know if it works for you as I am unable to test it at the moment.

Categories

Resources