我怎么能保存的WebView内容显示,即使在没有网络连接的情况? [英] How I can Save WebView Contents to show even when no network available?
问题描述
我创建一个应用程序,并使用的WebView为打开URL。我看到一些应用程序提供了保存页面(网页)选项来用户。
我想知道如何保存页面从我的WebView,这样我可以把它展示给用户时,它要求。
也许使用缓存是最好的方式......对,你应该检查的 http://developer.android.com/reference/android/webkit/WebSettings.html
管理设置状态的web视图。当一个web视图是第一 创建的,它获得一组默认设置。这些默认设置 将任何的getter调用返回。一个WebSettings对象获得 从WebView.getSettings()被连接到web视图的寿命。如果一个 WebView功能已被破坏,在WebSettings任何方法调用将抛出 一个IllegalStateException。
Especifically:
公共静态最终诠释** LOAD_CACHE_ONLY **
自:API级别1 不要使用网络负载仅从缓存。使用带setCacheMode(INT)。 常量值:3(0x00000003)
或
公共静态最终诠释** LOAD_CACHE_ELSE_NETWORK **
自:API级别1 使用高速缓存如果内容是存在的,即使过期(例如,历史导航)如果不是在高速缓存中,负载从网络。使用带setCacheMode(INT)。 常量值:1(00000001)
UPDATE1:
嗯,例如蹩脚$ C $所有生命的C:
公共静态的InputStream获取(字符串URL)抛出MalformedURLException的,
IOException异常{
DefaultHttpClient的HttpClient =新DefaultHttpClient();
HTTPGET请求=新HTTPGET(URL);
HTT presponse响应= httpClient.execute(要求);
返回response.getEntity()的getContent()。
}
私有静态字符串convertStreamToString(InputStream的是)
抛出IOException异常{
StringWriter的作家=新的StringWriter();
IOUtils.copy(是,作家,UTF-8);
返回writer.toString();
}
所以,你可以获取一个网址是的InputStream获取(字符串URL)的那么流转换为字符串,私有静态字符串 convertStreamToString(InputStream的是)并保存流式传送到一个文件中,以后可以加载内容到的WebView .....后来读
UPDATE2:
或者你可以做一些Java序列化的东西......不记得,如果这项工作在Android上的xD
发现了Java序列化API 的 http://java.sun.com/developer/technicalArticles/Programming/serialization/
I am creating a app and using WebView for open URL. I see some apps offer "Save Page"(A web page) option to user.
I want to know how to Save a page from my WebView so that I can show it to user when it request.
Maybe using a cache is the best way... for that you should check http://developer.android.com/reference/android/webkit/WebSettings.html
"Manages settings state for a WebView. When a WebView is first created, it obtains a set of default settings. These default settings will be returned from any getter call. A WebSettings object obtained from WebView.getSettings() is tied to the life of the WebView. If a WebView has been destroyed, any method call on WebSettings will throw an IllegalStateException."
Especifically:
public static final int **LOAD_CACHE_ONLY**
Since: API Level 1 Don't use the network, load from cache only. Use with setCacheMode(int). Constant Value: 3 (0x00000003)
Or
public static final int **LOAD_CACHE_ELSE_NETWORK**
Since: API Level 1 Use cache if content is there, even if expired (eg, history nav) If it is not in the cache, load from network. Use with setCacheMode(int). Constant Value: 1 (0x00000001)
Update1:
hmm "crappy code of all life" for example:
public static InputStream fetch(String url) throws MalformedURLException,
IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = httpClient.execute(request);
return response.getEntity().getContent();
}
private static String convertStreamToString(InputStream is)
throws IOException {
StringWriter writer = new StringWriter();
IOUtils.copy(is, writer, "UTF-8");
return writer.toString();
}
So, you can fetch a url with InputStream fetch(String url) an then convert that stream to String with private static String convertStreamToString(InputStream is) and save that stream to a file, later you can load that content to a webview..... to read later
Update2:
Or you can do some Java Serialization stuff... cant remember if this work on android xD
Discover the secrets of the Java Serialization API http://java.sun.com/developer/technicalArticles/Programming/serialization/
这篇关于我怎么能保存的WebView内容显示,即使在没有网络连接的情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!