4

当可以访问 Internet 时,我需要从 Internet 加载 mWebview。当它不可用时,它需要从缓存中加载它。我有一些东西,但它根本不加载网站

请帮我?

我已经添加了权限:
- android.permission.INTERNET
- android.permission.WRITE_EXTERNAL_STORAGE
- android.permission.ACCESS_NETWORK_STATE

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService( CONNECTIVITY_SERVICE );
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null;
}


@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Layout Webview
    mWebview  = new WebView(this);
    // Broadcast
    Toast.makeText(this, "Laden van roosterwijzigingen.",
    Toast.LENGTH_SHORT).show();
    // Enable JavaScript
    mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
    mWebview.setHorizontalScrollBarEnabled(false);
    mWebview.getSettings().setAppCacheMaxSize( 5 * 1024 * 1024 ); // 5MB
    mWebview.getSettings().setAppCachePath( getApplicationContext().getCacheDir().getAbsolutePath() );
    mWebview.getSettings().setAllowFileAccess( true );
    mWebview.getSettings().setAppCacheEnabled( true );
    mWebview.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT ); // load online by default
    final Activity activity = this;
    // Make WebClient
    mWebview.setWebViewClient(new WebViewClient() { 
    // Trace Errors
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
    Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
    }
}); 
    if ( !isNetworkAvailable() ) { // loading offline
        mWebview.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
    }

    mWebview.loadUrl("http://divers.ommelandercollege.nl/webportalen/dagrooster.php");

}
4

2 回答 2

0

我检查了你的代码并且缓存工作正常(用http://yahoo.com测试过)。您的问题必须在isNetworkAvailable()方法中,因此
setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK)不会调用。

要对此进行测试,请isNetworkAvailable()始终false暂时返回并在首次加载网页后将您的设备置于飞行模式。

于 2013-06-23T10:20:22.883 回答
-1
if ( !isNetworkAvailable() ) { // loading offline
    mWebview.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
style="color:red">}// move this bracket

mWebview.loadUrl("http://divers.ommelandercollege.nl/webportalen/dagrooster.php");
// to here  } 
}
于 2014-11-14T20:50:51.020 回答