我的应用程序显示RSS提要中的文章列表,当选择一个时,它将在Web视图中显示该文章.问题在于,Web视图显示的是台式机网站,而不是移动版本,我似乎无法弄清楚为什么.
WebVIEw代码:
package com.kentuckyfarmbureau.kyfb;import androID.app.Activity;import androID.content.Intent;import androID.graphics.Bitmap;import androID.net.Uri;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.webkit.WebChromeClIEnt;import androID.webkit.WebSettings;import androID.webkit.WebVIEw;import androID.webkit.WebVIEwClIEnt;import androID.webkit.WebSettings.RenderPriority;import androID.Widget.button;import androID.Widget.Imagebutton;import androID.Widget.Progressbar;import androID.Widget.Toast;public class Webbrowser extends Activity { WebVIEw web; Progressbar prgPageLoading; Imagebutton btnBack, btnForward; button btnShare; String myURL; /** Called when the activity is first created. */ @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.webbrowser); Intent iGet = getIntent(); myURL = iGet.getStringExtra("myURL"); web = (WebVIEw) findVIEwByID(R.ID.web); prgPageLoading = (Progressbar) findVIEwByID(R.ID.prgPageLoading); btnBack = (Imagebutton) findVIEwByID(R.ID.btnBack); btnForward = (Imagebutton) findVIEwByID(R.ID.btnForward); btnShare = (button) findVIEwByID(R.ID.btnShare); web.getSettings().setDomStorageEnabled(true); web.getSettings().setJavaScriptEnabled(true); web.getSettings().setSupportZoom(true); web.getSettings().setBuiltInZoomControls(false); web.getSettings().setUserAgentString("AndroID"); web.getSettings().setRenderPriority(RenderPriority.HIGH); web.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); web.loadUrl(myURL); btnShare.setonClickListener(new VIEw.OnClickListener() { //@OverrIDe public voID onClick(VIEw arg0) { // Todo auto-generated method stub String webUrl = web.getUrl(); String webTitle = web.getTitle(); final String p0 = "KYFB Share:"; final String p1 = "Kentucky Farm Bureau"; final String p2 = "'Big On Commitment.'"; Intent emailintent = new Intent(androID.content.Intent.ACTION_SEND); emailintent.setType("text/HTML"); emailintent.putExtra(androID.content.Intent.EXTRA_SUBJECT, webTitle); emailintent.putExtra(androID.content.Intent.EXTRA_TEXT, p0 + "\n\n" + webUrl + "\n\n\n" + p1 + "\n" + p2 + "\n\n"); startActivity(Intent.createChooser(emailintent, "Send your email in:")); } }); btnBack.setonClickListener(new OnClickListener() { public voID onClick(VIEw arg0) { // Todo auto-generated method stub if(web.canGoBack()){ web.goBack(); } else finish(); } }); btnForward.setonClickListener(new OnClickListener() { public voID onClick(VIEw v) { // Todo auto-generated method stub if(web.canGoForward()){ web.goForward(); } } }); final Activity act = this; web.setWebChromeClIEnt(new WebChromeClIEnt(){ public voID onProgressChanged(WebVIEw webvIEw, int progress){ act.setProgress(progress*100); prgPageLoading.setProgress(progress); } }); web.setWebVIEwClIEnt(new WebVIEwClIEnt() { @OverrIDe public voID onPageStarted( WebVIEw vIEw, String url, Bitmap favicon ) { super.onPageStarted( web, url, favicon ); prgPageLoading.setVisibility(VIEw.VISIBLE); } @OverrIDe public voID onPageFinished( WebVIEw vIEw, String url ) { super.onPageFinished( web, url ); prgPageLoading.setProgress(0); prgPageLoading.setVisibility(VIEw.GONE); } public voID onReceivedError(WebVIEw vIEw, int errorCode, String description, String failingUrl) { Toast.makeText(act, description, Toast.LENGTH_SHORT).show(); } public boolean shouldOverrIDeUrlLoading(WebVIEw vIEw, String url) { if(url.endsWith(".mp4") || url.endsWith(".3gp") || url.endsWith(".avi")){ Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); startActivity(i); //warning no error handling will cause force close if no media player on phone. return true; } vIEw.loadUrl(url); return true; } }); }}
解决方法:
正如@WarrenFaith在评论中指出的那样,这可能是由于您要使用的自定义User-Agent字符串引起的
web.getSettings().setUserAgentString("AndroID");
当接收到带有用户代理作为“ AndroID”的请求时,请不要设置此选项或确保您的Web服务器配置为可服务于网站的移动版本.
总结以上是内存溢出为你收集整理的Android WebView无法加载移动网站全部内容,希望文章能够帮你解决Android WebView无法加载移动网站所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)