首先检查互联网(您可以在 SO 上找到许多已解决的问题),如果有互联网连接,请使用 HttpClient 获取 HTML 源并将其保存到外部存储。然后在 webview 中从外部存储加载网页。如果没有互联网连接,只需在 webview 中加载页面。
互联网可用时保存和更新 html 源代码:
HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
HttpGet httpget = new HttpGet("http://yoururl.com"); // Set the action you want to do
HttpResponse response = httpclient.execute(httpget); // Executeit
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent(); // Create an InputStream with the response
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) // Read line by line
sb.append(line + "\n");
String resString = sb.toString(); // Result is here
is.close(); // Close the stream
File file = new File(Environment.getExternalStorageDirectory().toString()+"/Path/to/save/file/index.html");
file.createNewFile();
FileOutputStream f1 = new FileOutputStream(file, false);
PrintStream p = new PrintStream(f1);
p.print(resString);
p.close();
f1.close();
}catch(IOException e){}
编辑:正如@michaelcarrano 在评论中所说,使用另一个线程在后台执行这项工作。