保存html以供离线使用(Jsoup,CommonsIO)

时间:2017-10-03 13:19:43

标签: android html save jsoup apache-commons-io

我想保存一个html文件以供离线使用,因此使用Jsoup和CommonsIO。 (webview缓存对我来说没有选择)

首次启动时,html文件应该(显示在webview中)并下载并存储在设备的内部存储(或其他地方)。如果再次启动活动,则应从存储的html文件中接收数据。

main.cpp: In function 'int main(int, char**)':
main.cpp:23:24: error: use of deleted function 'IntContainer& IntContainer::operator=(const IntContainer&)'
     intContainers[0] = newIntContainer; // <-- Causes compiler error:
                        ^~~~~~~~~~~~~~~
main.cpp:2:8: note: 'IntContainer& IntContainer::operator=(const IntContainer&)' is implicitly deleted because the default definition would be ill-formed:
 struct IntContainer
        ^~~~~~~~~~~~
main.cpp:2:8: error: non-static const member 'const int IntContainer::value', can't use default assignment operator

AsyncTask下载并保存html文件。

public class Home extends Fragment {

View myView;
WebView myWebView;
Document doc;
String str;

@JavascriptInterface
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.home, container, false);
    super.onCreate(savedInstanceState);

    myWebView = myView.findViewById(R.id.webview2);
    myWebView.setWebViewClient(new WebViewClient());
    myWebView.getSettings().setJavaScriptEnabled(true);

    final SharedPreferences prefs = this.getActivity().getSharedPreferences("first", Context.MODE_PRIVATE);
    Boolean firstlauch = prefs.getBoolean("first", true);

    if (firstlauch){

        myWebView.loadUrl("https://en.wikipedia.org/wiki/Main_Page");

        Toast.makeText(getActivity(), "online", Toast.LENGTH_LONG).show();

        prefs.edit().putBoolean("first", false).apply();

        new doit().execute();

    } else {

        Toast.makeText(getActivity(), "offline", Toast.LENGTH_LONG).show();

        File getfile = new File("wikihome.html");

        try {
            str = FileUtils.readFileToString(getfile, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }

        myWebView.loadData(str, "text/html; charset=UTF-8", null);
    }

    return myView;
}

然而,当第二次启动Activity时,我得到的只是一个白色屏幕。

(显示直接从Jsoup解析的内容,所以我猜问题是在保存和检索文件的过程中的某个地方。)

注意:这是一个很好的解决方法,或者您是否建议我采用不同的方法来做到这一点?

0 个答案:

没有答案