[英]Android WebView not loading URL
I want to load the URL in WebView
我想在
WebView
加载URL
I have used the following Code:我使用了以下代码:
webView = (WebView) findViewById(R.id.webview1);
webView.setWebViewClient(new HostsWebClient());
webView.getSettings().setPluginState(PluginState.ON);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setSupportMultipleWindows(false);
webView.getSettings().setSupportZoom(false);
webView.setVerticalScrollBarEnabled(false);
webView.setHorizontalScrollBarEnabled(false);
webView.loadUrl(URL);
But when I execute it, I'm not able to load the url.但是当我执行它时,我无法加载 url。 I am getting web page not available.
我的网页不可用。
Could anyone help?有人可以帮忙吗?
Did you added the internet permission in your manifest file ?您是否在清单文件中添加了 Internet 权限? if not add the following line.
如果没有添加以下行。
<uses-permission android:name="android.permission.INTERNET"/>
hope this will help you.希望这会帮助你。
EDIT编辑
Use the below lines.使用以下几行。
public class WebViewDemo extends Activity {
private WebView webView;
Activity activity ;
private ProgressDialog progDailog;
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
activity = this;
progDailog = ProgressDialog.show(activity, "Loading","Please wait...", true);
progDailog.setCancelable(false);
webView = (WebView) findViewById(R.id.webview_compontent);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progDailog.show();
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, final String url) {
progDailog.dismiss();
}
});
webView.loadUrl("http://www.teluguoneradio.com/rssHostDescr.php?hostId=147");
}
}
Add Permission Internet permission in manifest.在清单中添加 Permission Internet 权限。
as <uses-permission android:name="android.permission.INTERNET"/>
作为
<uses-permission android:name="android.permission.INTERNET"/>
This code it working这段代码工作正常
public class WebActivity extends Activity {
WebView wv;
String url="http://www.teluguoneradio.com/rssHostDescr.php?hostId=147";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
wv=(WebView)findViewById(R.id.webUrl_WEB);
WebSettings webSettings = wv.getSettings();
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setPluginState(PluginState.ON);
wv.setWebViewClient(new myWebClient());
wv.loadUrl(url);
}
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
view.loadUrl(url);
return true;
}
}
maybe SSL也许 SSL
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
// ignore ssl error
if (handler != null){
handler.proceed();
} else {
super.onReceivedSslError(view, null, error);
}
}
Note : Make sure internet permission is given.注意:确保已授予 Internet 权限。
In android 9.0,在安卓 9.0 中,
Webview or Imageloader can not load url or image because android 9 have network security issue which need to be enable by manifest file for all sub domain. Webview 或 Imageloader 无法加载 url 或图像,因为 android 9 存在网络安全问题,需要通过清单文件为所有子域启用。 so either you can add security config file.
所以你可以添加安全配置文件。
<network-security-config> <domain-config cleartextTrafficPermitted="true"> <domain includeSubdomains="true">www.google.com</domain> </domain-config> </network-security-config>
<application android:networkSecurityConfig="@xml/network_security_config" ...> </application>
if you want to allow all sub domain如果你想允许所有子域
<application android:usesCleartextTraffic="true" ...> </application>
Note: To solve the problem, don't use both of point 2 ( android:networkSecurityConfig="@xml/network_security_config"
and android:usesCleartextTraffic="true"
) choose one of them注意:要解决问题,请不要同时使用第 2 点(
android:networkSecurityConfig="@xml/network_security_config"
和android:usesCleartextTraffic="true"
)选择其中之一
First, check if you have internet permission in Manifest file.首先,检查您在 Manifest 文件中是否具有 Internet 权限。
<uses-permission android:name="android.permission.INTERNET" />
You can then add following code in onCreate() or initialize() method-然后,您可以在 onCreate() 或 initialize() 方法中添加以下代码-
final WebView webview = (WebView) rootView.findViewById(R.id.webview);
webview.setWebViewClient(new MyWebViewClient());
webview.getSettings().setBuiltInZoomControls(false);
webview.getSettings().setSupportZoom(false);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.getSettings().setAllowFileAccess(true);
webview.getSettings().setDomStorageEnabled(true);
webview.loadUrl(URL);
And write a class to handle callbacks of webview -并编写一个类来处理 webview 的回调 -
public class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//your handling...
return super.shouldOverrideUrlLoading(view, url);
}
}
in same class, you can also use other important callbacks such as -在同一类中,您还可以使用其他重要的回调,例如 -
- onPageStarted()
- onPageFinished()
- onReceivedSslError()
Also, you can add "SwipeRefreshLayout" to enable swipe refresh and refresh the webview.此外,您可以添加“SwipeRefreshLayout”以启用滑动刷新并刷新webview。
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
And refresh the webview when user swipes screen:并在用户滑动屏幕时刷新 webview:
SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(false);
webview.reload();
}
}, 3000);
}
});
Use the following things on your webview在您的 webview 上使用以下内容
webview.setWebChromeClient(new WebChromeClient());
then implement the required methods for WebChromeClient class.然后为 WebChromeClient 类实现所需的方法。
Just as an alternative solution:作为替代解决方案:
For me webView.getSettings().setUserAgentString("Android WebView")
did the trick.对我来说
webView.getSettings().setUserAgentString("Android WebView")
做到了。
I already had implemented INTERNET permission and WebViewClient
as well as WebChromeClient
我已经实现了 INTERNET 权限和
WebViewClient
以及WebChromeClient
The simplest solution is to go to your XML layout containing your webview.最简单的解决方案是转到包含 webview 的 XML 布局。 Change your android:layout_width and android:layout_height from "wrap_content" to "match_parent".
将您的 android:layout_width 和 android:layout_height 从“wrap_content”更改为“match_parent”。
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"/>
In my Case, Adding the below functions to WebViewClient fixed the error.在我的案例中,将以下函数添加到 WebViewClient 修复了错误。 the functions are:
onReceivedSslError
and Depricated and new api versions of shouldOverrideUrlLoading
该功能是:
onReceivedSslError
和Depricated和新的API版本shouldOverrideUrlLoading
webView.setWebViewClient(new WebViewClient() {
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
Log.i(TAG, "loading: deprecation");
return true;
//return super.shouldOverrideUrlLoading(view, url);
}
@Override
@TargetApi(Build.VERSION_CODES.N)
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
Log.i(TAG, "loading: build.VERSION_CODES.N");
return true;
//return super.shouldOverrideUrlLoading(view, request);
}
@Override
public void onPageStarted(
WebView view, String url, Bitmap favicon) {
Log.i(TAG, "page started:"+url);
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, final String url) {
Log.i(TAG, "page finished:"+url);
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError er) {
handler.proceed();
}
});
Use this it should help.使用它应该会有所帮助。
var currentUrl = "google.com"
var partOfUrl = currentUrl.substring(0, currentUrl.length-2)
webView.setWebViewClient(object: WebViewClient() {
override fun onLoadResource(WebView view, String url) {
//call loadUrl() method here
// also check if url contains partOfUrl, if not load it differently.
if(url.contains(partOfUrl, true)) {
//it should work if you reach inside this if scope.
} else if(!(currentUrl.startWith("w", true))) {
webView.loadurl("www.$currentUrl")
} else if(!(currentUrl.startWith("h", true))) {
webView.loadurl("https://$currentUrl")
} else {
//...
}
}
override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
// you can call again loadUrl from here too if there is any error.
}
// You should also override other override method for error such as
// onReceiveError to see how all these methods are called one after another and how
// they behave while debugging with break point.
}
对于某些网页,关键是启用DOM 存储:
webview.getSettings().setDomStorageEnabled(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.