MikeB
MikeB

Reputation: 31

webpage with room database..?

How is it possible to save a webpage content that I read with webView inside room database.

I guess saving the cache isn't an option for a database I can't find any different option or any example that I can save a webpage content inside a database for offline View ..>!

So, any help or example or suggestion?

thanks in advance..!

Maybe it is possible at all please provide me information because I have a Task and I don't know if I'm thinking right about the situation ...!

I saw this option but like I said its cache saving and there isn't an example to save it inside Room database.

WebView webView = new WebView( context );
webView.getSettings().setAppCacheMaxSize( 5 * 1024 * 1024 ); // 5MB
webView.getSettings().setAppCachePath( getApplicationContext().getCacheDir().getAbsolutePath() );
webView.getSettings().setAllowFileAccess( true );
webView.getSettings().setAppCacheEnabled( true );
webView.getSettings().setJavaScriptEnabled( true );
webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT ); // load online by default

if ( !isNetworkAvailable() ) { // loading offline
webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
}

webView.loadUrl( "http://www.google.com" );

Upvotes: 0

Views: 307

Answers (1)

NIKHIL MAURYA
NIKHIL MAURYA

Reputation: 304

What you need to do is download the html content from the url as string and save it inside the room database. you can use okhttp of that like below

                String url = "https://google.com";
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url)
                        .build();

                client.newCall(request).enqueue(new Callback() {

                    @Override
                    public void onFailure(Call call, IOException e) {

                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (response.isSuccessful()) {

                            final String aFinalString = response.body().string();
//Save this string in your room database



                        } else {
                        }
                    }
                });

TO show the webpage you just downloaded you can try this

webView.loadData(yourData, "text/html; charset=utf-8", "UTF-8");

Where yourData would be the string retrieved from Room DB

Upvotes: 1

Related Questions