Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving data from a WebView

I need to save data that I show on WebView. I have achieved that with saving all of my URL requests that are made in WebViewClient and send them to backend service. Back end service later make requests for those files.

The problem is that WebViewClient is running its own cycle and responses are not visible which leads me to make two requests for each resource.

Is there any way for getting data directly inside WebViewClient?

Here is piece of current code, which is working:

private class Client extends WebViewClient{
@Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        plainText = //get plain text and save it
        webView.loadDataWithBaseURL(url, plainText, MIME_TYPE_TEXT_HTML, UTF_8_ENCODING, null);
        return true;
    }
    @SuppressWarnings("deprecation")
    @Override
    public synchronized WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        // we need to make sure it does nothing in this one for >= lollipop as in lollipop the call goes through both methods
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            return interceptRequest(view, url, null);
        }
        return null;
    }
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        WebResourceResponse response = super.shouldInterceptRequest(view, request);
        final String url = request.getUrl().toString();
        return interceptRequest(view, url, response);
    }

    private WebResourceResponse interceptRequest(WebView webView, String url, WebResourceResponse webResourceResponse) {
        addUrlToDownloadQueue(url);
        return webResourceResponse;
    }

addUrlToDownloadQueue(url); is taking care that all urls are passed to service which retrieve all sources.

Is there any way to obtain data on WebViewClient without passing them to backend service?

like image 800
5er Avatar asked May 05 '16 08:05

5er


1 Answers

Have you read this previous question?

In case you are not able to achieve your goal let me know.

like image 186
Lorenzo Camaione Avatar answered Oct 28 '22 08:10

Lorenzo Camaione