0

我正在尝试在 web 视图中加载 google 日历公共日历的 url。最初它没有问题。在某些时候,我得到的只是 webview 中的文本“null”。我也尝试将网址更改为具有相同结果的基本网页。我也尝试注释掉 wbv.loadurl 行并得到相同的结果。我尝试单步执行代码,并且在未注释掉时调用了该行。任何帮助,将不胜感激。下面是填充 webview 的代码:

wbv = (WebView) findViewById(R.id.audioPlayer_showSchedule);
WebSettings settings = wbv.getSettings();
wbv.setWebViewClient(new TwitWebViewClient());
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
settings.setJavaScriptEnabled(true);
settings.setDefaultTextEncodingName("utf-8");
wbv.loadUrl("http://www.google.com/calendar/embed?src=r8psn8mpajnfa2703k43l6o014@group.calendar.google.com&ctz=America/Los_Angeles&program&mode=Week&gsessionid=vzeVWSq2Wdk3eVmGOUp1bQ");
4

2 回答 2

1

使用下面的工作代码::

public class Android_Activity extends Activity {
private Android_Activity _activity;
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);     
            getWindow().requestFeature(Window.FEATURE_PROGRESS);
            _activity = this;   
            setContentView(R.layout.main);

            mwebview=(WebView)view.findViewById(R.id.webview);
            mwebview.getSettings().setJavaScriptEnabled(true);
            mwebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

            if(checkInternetConnection(_activity)==true){
                if(savedInstanceState==null)
                    mwebview.loadUrl("http://abc.com");
                else
                    mwebview.restoreState(savedInstanceState);
            }
            else{
                AlertDialog.Builder builder = new AlertDialog.Builder(_activity);
                builder.setMessage("Please check your network connection.")
                       .setCancelable(false)
                       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {

                           }
                       });

                AlertDialog alert = builder.create();    
                alert.show();
            }
            mwebview.setWebChromeClient(new WebChromeClient() {

                @Override
                public void onProgressChanged(WebView view, int progress) { 
                    if(mwebview.getVisibility()==View.VISIBLE)
                    {
                        _activity.setProgress(progress * 100);
                    }
                }
            });
            mwebview.setWebViewClient(new HelloWebViewClient());
        }


        //HelloWebViewClient class for webview
        private class HelloWebViewClient extends WebViewClient {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                super.onPageStarted(view, url, favicon);
            }
            @Override
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                // TODO Auto-generated method stub
                super.onReceivedError(view, errorCode, description, failingUrl);

            }
            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);
            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
                view.loadUrl(url);
                return true;
            }

        }   //HelloWebViewClient-class
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            // Check if the key event was the Back button and if there's history
            if ((keyCode == KeyEvent.KEYCODE_BACK) && mwebview.canGoBack() ){
                mwebview.goBack();
                return true;
            }
            // If it wasn't the Back key or there's no web page history, bubble up to the default
            // system behavior (probably exit the activity)
            return super.onKeyDown(keyCode, event);
        }
        //To check whether network connection is available on device or not
            public static boolean checkInternetConnection(Activity _activity) {
                ConnectivityManager conMgr = (ConnectivityManager) _activity.getSystemService(Context.CONNECTIVITY_SERVICE);
                if (conMgr.getActiveNetworkInfo() != null
                        && conMgr.getActiveNetworkInfo().isAvailable()
                        && conMgr.getActiveNetworkInfo().isConnected()) 
                    return true;
                else
                    return false;
            }//checkInternetConnection()
}

而且在你的 main.xml 布局中,你必须有一个 id 为 webview 的 webview

于 2012-04-04T07:18:41.700 回答
0

这一切对我来说都很好。你确定你TwitWebViewClient正在做你期望的事情吗?

另外,您确定这是您要加载的 URL 吗?我不禁注意到"gsessionid=vzeVWSq2Wdk3eVmGOUp1bQ"它的结尾。一旦您的会话到期,这肯定不会起作用吗?

于 2012-04-04T07:20:41.677 回答