9

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?

Yazan
  • 6,074
  • 1
  • 19
  • 33
5er
  • 2,506
  • 7
  • 32
  • 49
  • I haven't fully understood what you want to achieve. Do you want to read/get data from WebView or do you want to insert data into WebView? – Lorenzo Camaione May 07 '16 at 13:47
  • 1
    possible duplicate of http://stackoverflow.com/questions/2376471/how-do-i-get-the-web-page-contents-from-a-webview – Yazan May 08 '16 at 06:24
  • @LorenzoCamaione, well both. First time visiting a web page a want read/get data, and next time visiting specific web page want to insert data into WebView – 5er May 09 '16 at 11:50
  • @Yazan, I am not dealing with javascript.. thats handled.. – 5er May 09 '16 at 11:51
  • What's the need you want to meet? – tiny sunlight May 11 '16 at 15:12
  • @tinysunlight, I want reduce the amount of internet traffic. Also when user is offline, I want to show him saved data of previous visited web pages. – 5er May 12 '16 at 11:56
  • Why don't you just use webview cache? – tiny sunlight May 12 '16 at 15:33
  • when user is gonna close app all data are lost... You can not manipulate cache in term of saving it permanent. – 5er May 12 '16 at 18:17
  • Even if you send it to backend i fail to understand how does it satisfy your offline scenario. Either you have to use cache or some trick with javascript to get what you wanna do and still offline scenario is not gonna be handled. – frogEye May 13 '16 at 12:56
  • 1
    @Vishal, My backend service save all .js .css .hml ..etc files with their names. When user is offline all those files are there saved on the phone. Instead showing user offline page I can show them saved pages. I don't have to use any java script. instead of *webView.loadUrl(url)* I call *webView.loadDataWithBaseURL(url, data, mimeType, encoding, mHistoryUrl)* – 5er May 17 '16 at 08:39
  • http://stackoverflow.com/questions/32305414/android-save-data-from-webview – ben10 Jun 02 '16 at 10:27

2 Answers2

1

I'm posting another answer because this answer is too long to suit in a comment.

Well, that's not good to follow that question if you doesn't control the web page. In fact as said in android developer guide: "Caution: Using addJavascriptInterface() allows JavaScript to control your Android application. This can be a very useful feature or a dangerous security issue. When the HTML in the WebView is untrustworthy (for example, part or all of the HTML is provided by an unknown person or process), then an attacker can include HTML that executes your client-side code and possibly any code of the attacker's choosing."

To get data in the web page you can use webView.loadUrl("javascript: document.getElementById("example").value;");

To get data from the web page try to refer to this page.

If you need more help contact me!!

Community
  • 1
  • 1
Lorenzo Camaione
  • 505
  • 3
  • 15
  • onPageFinished is not guaranteed to be called every time... Web page may fail. Also that does not save content of all external calls. – 5er May 11 '16 at 08:18
  • It's possible!!!! In fact you should some checks before doing anything in the web view. Are you able to do it? – Lorenzo Camaione May 11 '16 at 08:28
1

see my example to obtain data before submit :
https://github.com/henrychuangtw/WebView-Javascript-Inject

and stop to pass data to backend, return false in onsubmit function

webview1.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);

        StringBuilder sb = new StringBuilder();
        sb.append("document.getElementsByTagName('form')[0].onsubmit = function () {");
        sb.append("var objPWD, objAccount;var str = '';");
        sb.append("var inputs = document.getElementsByTagName('input');");
        sb.append("for (var i = 0; i < inputs.length; i++) {");
        sb.append("if (inputs[i].type.toLowerCase() === 'password') {objPWD = inputs[i];}");
        sb.append("else if (inputs[i].name.toLowerCase() === 'email') {objAccount = inputs[i];}");
        sb.append("}");
        sb.append("if (objAccount != null) {str += objAccount.value;}");
        sb.append("if (objPWD != null) { str += ' , ' + objPWD.value;}");
        sb.append("window.MYOBJECT.processHTML(str);");
        sb.append("return true;"); //return false, to stop onsubmit function
        sb.append("};");

        view.loadUrl("javascript:" + sb.toString());
    }
});
Badro Niaimi
  • 959
  • 1
  • 14
  • 28
HenryChuang
  • 1,449
  • 17
  • 28