2013-02-26 18 views
1

更新的WebView我有一個應用程序,從網絡上獲得一個XML文件,對其進行解析中檢索路徑的網站我有一個網頁,以cache.eg如何從的AsyncTask

/services/orthopaedics.php 

。 我可以成功下載需要緩存的所有頁面的路徑。目前我在AsyncTask中執行此操作。 Web服務調用(頁面路徑)的結果存儲在AsyncTask的doInBackground方法中的contentsValues中。然後我遍歷onPostExecute()方法中的這些contentsvalues,我可以逐個加載webview的loadUrl()方法中的網頁。這個webview是隱形的,因爲它只用於緩存目的。

問題是,因爲不可見的web視圖正在加載用戶可以看到的另一個webview,並且顯示網站的索引頁面,所以加載速度很慢。出現一個白色屏幕,直到不可見的webview已停止加載緩存。在phoe上,大約需要4秒,這是可以接受的,但是在android平板電腦上大約需要30秒。

我知道調用webview必須在UI線程上,這就是爲什麼我加載onPostExecute方法中的網頁(隱形webview)。是否有可能以某種方式將頁面加載到doInBacground方法中的緩存中,但將webview.loadUrl()放入runOnUiThread(新的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); 



    } 

回答

3

我認爲唯一的解決辦法是,在doInBackground一個HTTP請求,然後在publishProgress使用 loadData來加載數據。

+0

你能告訴我該怎麼做 – mohitum 2014-02-13 13:40:34

2

不幸的是,你不能從AsyncTask「加載」一個URL。必須從主線程調用所有「WebView」方法。

你所能做的就是在第二個線程中更新進度對話框。檢查這個答案Webview with asynctask on Android