0

Instead of implement my website as an android app I use my website to show it in the app via webview. I have search and search and found similar question but none give me a solution that works... I want to grab the post data from webview when use a webpage where the user press submit. I got it to work in iOS with swift where i use this solution: Grabbing POST data from UIWebView

What I have tried so far: But this return raw html.

    wv.setWebViewClient(new WebViewClient(){

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {

            Log.d(LOGTAG, url);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(flag)
            {
                try {
                    URL aURL = new URL(url);
                    HttpURLConnection conn = (HttpURLConnection) aURL.openConnection();
                    conn.connect();
                    InputStream is = conn.getInputStream();

                    BufferedReader streamReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    StringBuilder response = new StringBuilder();
                    String inputLine;
                    while ((inputLine = streamReader.readLine()) != null) {
                        response.append(inputLine);
                    }
                    is.close();

                    String s = response.toString();

                    Log.d(LOGTAG, "response" + s);

                    view.loadUrl(url);

                    return true;

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    return false;
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                } 
            }

            return false;

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            if(url.contains("form.html"))
            {
                flag = true;
            }


        }
    });

    wv.loadUrl(mainUrl);
Community
  • 1
  • 1
Loc Dai Le
  • 1,661
  • 4
  • 35
  • 70

1 Answers1

0

The response what your getting correct. The below code will hit the url and its as good as loading the webpage. So your html data in inputstream.

         HttpURLConnection conn = (HttpURLConnection) aURL.openConnection();
                conn.connect();
                InputStream is = conn.getInputStream();

Solution to your Problem(Javascript bridge). Android supports Javascript Bridge you can write javascripts by which data can be captured.

Upadte

currently based on your problem 2 solutions can suggest as best i know.

  1. Make changes at website (html page, web environment), like write a javascript at on click handler (where you need to trigger capture post data) capture the data required and then pass it to android javascript bridge (android java code, android java environment)which tied to the webview of the loading html page. With this approach changes has to be made both places and html page changes need to aligned with javacode changes(javascript bridges names)
  2. Javascript Injection - understand the html page and do the javascript injection.. look at this ans
Community
  • 1
  • 1
Sush
  • 3,864
  • 2
  • 17
  • 35
  • I really appreciate your answer @sush. I'm new to Java/Android so I have no clue how to do this. I have been stuck with this problem for a few days now. Is it possible you can guide me further - how to use this Javescript Bridge? Thank you. – Loc Dai Le Oct 29 '15 at 12:13