Android Application Development - WebView Not Working

Android allows you to display the contents of a URL in an application using WebView . However, for some reason this does not work for me. The following is the code I'm using:

package com.news; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.webkit.WebView; import android.webkit.WebViewClient; public class NewsActivity extends Activity { WebView mWebView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mWebView = (WebView) findViewById(R.id.webview); mWebView.setWebViewClient(new NewsClient()); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setDomStorageEnabled(true); mWebView.loadUrl("http://www.androidpeople.com"); } public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { mWebView.goBack(); return true; } return super.onKeyDown(keyCode, event); } private class NewsClient extends WebViewClient { public boolean shouldOverrideUrlLoading(WebView view, String url) { System.out.println("URL: " + url); view.loadUrl(url); return true; } } } 
+8
source share
5 answers

It is obvious!

You will implement a new WebViewClient in which you override the shouldOverrideUrlLoading method. This method is called for every URL that you download. And what are you doing there? You return true (which means the download must be redefined) and then start loading the same URL! Thus, URL loading will never happen.

Just delete this line:

 mWebView.setWebViewClient(new NewsClient()); 
+4
source

add this to android manifest if it is not added:

 <uses-permission android:name="android.permission.INTERNET" /> 

this line should be inside the <manifest> element of your AndroidManifest.xml file.

0
source

Add this line: mWebView.setWebViewClient(new NewsClient());

But shouldOverrideUrlLoading should return false .

0
source

Your code works on my Android 4.2.2 device, there are no problems in your implementation, the only thing you need to do is override your method as follows:

  @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { System.out.println("URL: " + url); view.loadUrl(url); return true; } 

This method

0
source

I think you can just load the url into your webview to show the website in your application as

 webView.loadUrl(url); 

as well as resolution is the most important

 <uses-permission android:name="android.permission.INTERNET"/> 

for more details: http://androidcoding.in/2016/03/17/android-tutorial-on-webview-for-beginners/

0
source

Source: https://habr.com/ru/post/1345219/


All Articles