[英]Android WebView not loading URL
我想在WebView
加载URL
我使用了以下代码:
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);
但是当我执行它时,我无法加载 url。 我的网页不可用。
有人可以帮忙吗?
您是否在清单文件中添加了 Internet 权限? 如果没有添加以下行。
<uses-permission android:name="android.permission.INTERNET"/>
希望这会帮助你。
编辑
使用以下几行。
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");
}
}
在清单中添加 Permission Internet 权限。
作为<uses-permission android:name="android.permission.INTERNET"/>
这段代码工作正常
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;
}
}
也许 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);
}
}
注意:确保已授予 Internet 权限。
在安卓 9.0 中,
Webview 或 Imageloader 无法加载 url 或图像,因为 android 9 存在网络安全问题,需要通过清单文件为所有子域启用。 所以你可以添加安全配置文件。
<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>
如果你想允许所有子域
<application android:usesCleartextTraffic="true" ...> </application>
注意:要解决问题,请不要同时使用第 2 点( android:networkSecurityConfig="@xml/network_security_config"
和android:usesCleartextTraffic="true"
)选择其中之一
首先,检查您在 Manifest 文件中是否具有 Internet 权限。
<uses-permission android:name="android.permission.INTERNET" />
然后,您可以在 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);
并编写一个类来处理 webview 的回调 -
public class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//your handling...
return super.shouldOverrideUrlLoading(view, url);
}
}
在同一类中,您还可以使用其他重要的回调,例如 -
- onPageStarted()
- onPageFinished()
- onReceivedSslError()
此外,您可以添加“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>
并在用户滑动屏幕时刷新 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);
}
});
在您的 webview 上使用以下内容
webview.setWebChromeClient(new WebChromeClient());
然后为 WebChromeClient 类实现所需的方法。
作为替代解决方案:
对我来说webView.getSettings().setUserAgentString("Android WebView")
做到了。
我已经实现了 INTERNET 权限和WebViewClient
以及WebChromeClient
最简单的解决方案是转到包含 webview 的 XML 布局。 将您的 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"/>
在我的案例中,将以下函数添加到 WebViewClient 修复了错误。 该功能是: 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();
}
});
使用它应该会有所帮助。
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.