gpt4 book ai didi

android - 如何从 AsyncTask 更新 webview

转载 作者:行者123 更新时间:2023-11-29 15:23:50 25 4
gpt4 key购买 nike

我有一个从网络获取 xml 文件的应用程序,解析它以检索我必须缓存的网站网页的路径。例如

/services/orthopaedics.php

.我可以成功下载所有需要缓存的页面的路径。目前我在 AsyncTask 中执行此操作。 Web 服务调用的结果(页面路径)存储在 AsyncTask 的 doInBackground 方法中的 contentsValues 中。然后我在 onPostExecute() 方法中遍历这个 contentsvalues,我可以在 webview 的 loadUrl() 方法中一个一个地加载网页。此 webview 是不可见的,因为它仅用于缓存目的。

问题在于,由于不可见的 webview 正在加载用户可以看到的另一个 webview,并且要显示加载缓慢的网站的索引页面。在不可见的 webview 停止加载缓存之前,会出现一个白色屏幕。在 phoe 上大约需要 4 秒,这是可以接受的,但在 android 平板电脑上大约需要 30 秒。

我知道对 webview 的调用必须在 UI 线程上,这就是为什么我在 onPostExecute 方法中加载网页(不可见的 webview)。是否可以在 doInBacground 方法中以某种方式将页面加载到缓存中,但将 webview.loadUrl() 放在 runOnUiThread(new Runnable) 中?

我将在下面给出我的意思的简化示例。

private class AsyncCacheSite extends AsyncTask<String, ContentValues, ContentValues>{

ProgressDialog progressDialog;

@Override
protected void onPreExecute() {
super.onPreExecute();
Log.e(TAG, "about to load main page");
webView.loadUrl(mobileWebsiteUrl, getHeaders());

progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Connecting to Server");
progressDialog.setMessage("Caching website for offline use...");
progressDialog.setIndeterminate(true);
progressDialog.show();
}

@Override
protected ContentValues doInBackground(String... params) {
ContentValues cv = null;

try {
//call the service to get the xml file containing paths
cv = ws.checkForUpdates();


} catch (Exception e) {
e.printStackTrace();
}

//iterate through the xml file geting the paths
.......
.......
.......
runOnUiThread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
loadUrlIntoCache(page);

}
})


return null;

}



@Override
protected void onPostExecute(ContentValues result) {
super.onPostExecute(result);



progressDialog.dismiss();


}//end of postExecute

}//end of asyncTask



public void loadUrlIntoCache(String pageUrl){

WebView wv2 = new WebView(MainActivity.this);

wv2.getSettings().setSupportZoom(true);
wv2.getSettings().setBuiltInZoomControls(true);
wv2.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
wv2.setScrollbarFadingEnabled(true);
wv2.getSettings().setLoadsImagesAutomatically(true);
wv2.getSettings().setDomStorageEnabled(true);
wv2.getSettings().setAppCacheEnabled(true);
// Set cache size to 8 mb by default. should be more than enough
wv2.getSettings().setAppCacheMaxSize(1024*1024*32);
// This next one is crazy. It's the DEFAULT location for your app's cache
// But it didn't work for me without this line.
// UPDATE: no hardcoded path. Thanks to Kevin Hawkins
String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();

wv2.getSettings().setAppCachePath(appCachePath);
wv2.getSettings().setAllowFileAccess(true);
wv2.getSettings().setJavaScriptEnabled(true);
// wv2.setWebViewClient(new WebViewClient());

////////////////////////////////////////////////////////////////////////////////////////
/////the webviewclient below is the code you gave me to overcome the redirecting issue//
////////////////////////////////////////////////////////////////////////////////////////
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
// do your handling codes here, which url is the requested url
// probably you need to open that url rather than redirect:
view.loadUrl(url);
return false; // then it is not handled by default action
}
});

wv2.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
wv2.setVisibility(View.GONE);

wv2.loadUrl(mobileWebsiteUrl + pageUrl, getHeaders());
Log.e(TAG, "loading " + mobileWebsiteUrl + pageUrl);



}

最佳答案

我认为唯一的解决方案是在 doInBackground 中使用 http 请求加载数据,然后使用 loadData在 publishProgress 中。

关于android - 如何从 AsyncTask 更新 webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15095042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com