Thursday, 27 July 2017

Loading custom headers on every page load inside a web view

Sometimes we have a requirement to send custom headers while opening a webview and also pass same headers whenever user navigates to another webpage within the webview. So here's how we can achieve that :


private class MyWebViewClient extends WebViewClient {
       @Override
       public void onPageStarted(WebView webView, String url, Bitmap favicon) {
           super.onPageStarted(webView, url, favicon);
       }

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

       @Override
       public void onLoadResource(WebView view, String url) {
       }

       // api less then 24
       @SuppressWarnings("deprecation")
       @Override
       public boolean shouldOverrideUrlLoading(WebView view, String url) {
           return view.load(url,yourheaders);
       }

       // api greater than 24
       @TargetApi(Build.VERSION_CODES.N)
       @Override
       public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
           return view.load(request.getUrl(),yourheaders);
   }

   }

No comments:

Post a Comment