3

I'm currently developing a login module. After click the "login" button in a WebView, the browser will receive a json response, and then redirect to the another page. How can I intercept this json response?

I tried shouldOverrideUrlLoading (WebView view, String url) and print all the url input, but I can not find the json response.

mWebview.setWebViewClient(new WebViewClient() {

        @Override
        public WebResourceResponse shouldInterceptRequest (final WebView view, String url) {
            Log.i("InterceptRequest", url);
            return super.shouldInterceptRequest(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            Log.i("onPageStarted", url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            Log.i("onPageFinished", url);
        }

    });

I also noticed that when I can not see this response in Chrome and Firefox, but I can see it in IE.

Thank you.

Linshen Qi
  • 79
  • 2
  • 7

1 Answers1

0

Override shouldOverrideUrlLoading on the webclient :

@Override
public boolean shouldOverrideUrlLoading (WebView view, String url) {

    URL aURL = new URL(url); 
            URLConnection conn = aURL.openConnection(); 
            conn.connect(); 
            InputStream is = conn.getInputStream(); 
            // read inputstream to get the json..
            ...
            ...
            return true;
}
Juan Hurtado
  • 328
  • 1
  • 7
  • ignore the flag is from a non-related code :P, try again with the edited code – Juan Hurtado Aug 03 '16 at 00:00
  • But this url is the main page that has been redirected to after login:( – Linshen Qi Aug 03 '16 at 17:36
  • 2
    I don't know if this was valid at the time of writing but it is not anymore. The documentation explicitly states "Note: This method is not called for POST requests." https://developer.android.com/reference/android/webkit/WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView,%20android.webkit.WebResourceRequest) – Klitos G. Jan 21 '20 at 17:16
  • 1
    This doesn't solution to catch json response. This is catch url and manual to excute other request to server. – Phan Sinh Jun 08 '20 at 07:27