的WebView显示不正确的网站 [英] WebView not showing website correctly
本文介绍了的WebView显示不正确的网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
的WebView显示不正确的网站。任何帮助将是巨大的!
在code香港专业教育学院使用的所有其他网站的工作。不知道什么问题。任何事情,我要补充的吗?在Chrome的工作以及和其他浏览器,因此不知道该怎么办。任何帮助将是巨大的!
的WebView
镀铬
公共类网站延伸活动{
的WebView myWebView;
的LinearLayout根;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.website); myWebView =(的WebView)findViewById(R.id.webView);
myWebView.loadUrl(http://dspart.org); 根=(的LinearLayout)findViewById(R.id.root); WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(真); myWebView.addJavascriptInterface(新WebAppInterface(本),机器人);
myWebView.setWebViewClient(新WebViewClient());
。myWebView.getSettings()setBuiltInZoomControls(真);
。myWebView.getSettings()setSupportZoom(真);
。myWebView.getSettings()setUseWideViewPort(真);
。myWebView.getSettings()setLoadWithOverviewMode(真); getActionBar()setDisplayHomeAsUpEnabled(真)。
}
公共类WebAppInterface {
上下文mContext; / **实例化界面和设置上下文* /
WebAppInterface(上下文C){
mContext = C;
} / **显示从网页举杯* /
@JavascriptInterface
公共无效showToast(字符串吐司){
Toast.makeText(mContext,烤面包,Toast.LENGTH_SHORT).show();
}
私有类MyWebViewClient扩展WebViewClient {
@覆盖
公共布尔shouldOverrideUrlLoading(的WebView视图,字符串URL){
如果(Uri.parse(URL).getHost()。等于(http://dspart.org)){
//这是我的网站,所以不要覆盖;让我的WebView加载页面
返回false;
}
//否则,该链接不上我的网站页面,因此启动该处理URL另一活动
意向意图=新意图(Intent.ACTION_VIEW,Uri.parse(URL));
startActivity(意向);
返回true;
} }
}
@覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件){
如果(event.getAction()== KeyEvent.ACTION_DOWN){
开关(键code){
案例KeyEvent.KEY code_BACK:
如果(myWebView.canGoBack()){
myWebView.goBack();
}
其他{
root.removeView(myWebView);
myWebView.removeAllViews();
myWebView.destroy();
this.finish();
}
返回true;
}
}
返回super.onKeyDown(键code,事件);
} @覆盖
公共布尔onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例android.R.id.home:
NavUtils.navigateUpFromSameTask(本);
root.removeView(myWebView);
myWebView.removeAllViews();
myWebView.destroy();
this.finish();
返回true;
默认:
返回super.onOptionsItemSelected(项目); }
}}
解决方案
如果(Uri.parse(URL).getHost()。等于(http://dspart.org )){
而不是上面一行,使用 -
URL aUrl =新的URL(http://dspart.org)如果(Uri.parse(URL).getHost()。equalsIgnoreCase(aUrl.getHost())){
这样,你不承担机会是否和getHost()
在 Uri.parse(URL)
将返回dspart.org或 http://dspart.org 。
试试看吧和恢复。
WebView is not showing website correctly. Any help would be great! The code ive used work on all another site. Not sure whats the issue. Any thing I should add? Works well in chrome and other browsers so don't know what to do. Any help would be great!
WebView
Chrome
public class Website extends Activity {
WebView myWebView;
LinearLayout root;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.website);
myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl("http://dspart.org");
root = (LinearLayout) findViewById(R.id.root);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("http://dspart.org")) {
// 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;
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode) {
case KeyEvent.KEYCODE_BACK:
if(myWebView.canGoBack()) {
myWebView.goBack();
}
else {
root.removeView(myWebView);
myWebView.removeAllViews();
myWebView.destroy();
this.finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
root.removeView(myWebView);
myWebView.removeAllViews();
myWebView.destroy();
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}}
解决方案
if (Uri.parse(url).getHost().equals("http://dspart.org")) {
instead of above line, use -
Url aUrl = new URL("http://dspart.org")
if (Uri.parse(url).getHost().equalsIgnoreCase(aUrl.getHost())) {
this way you are not taking chances on whether getHost()
on your Uri.parse(url)
will return dspart.org or http://dspart.org.
Give it a try and revert.
这篇关于的WebView显示不正确的网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文