title: Send authorization header with every request in webview using okhttp in android
body: <p>I am using WebView to display web page, but the server expects an authorization token with every request from my webview. Anybody know if this is possible ? 
  I referred this post in #SO <a href="https://stackoverflow.com/questions/32605711/adding-header-to-all-request-with-retrofit-2">adding-header-to-all-request-with-retrofit-2</a>. But I'm not able to get the result. </p>
  
  <p>This is my code (bare with my coding standard, I'm a beginner)</p>
  
  <pre><code>    public class TableViewTest extends AppCompatActivity {
  
      ScrollView scrollView;
      WebView webView;
      SharedPreferences pref;
  
      @SuppressLint("SetJavaScriptEnabled")
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_table_view_test);
  
          //final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...", true);
  
  
          webView = (WebView) findViewById(R.id.webView1Id)
  
          webView.setVerticalScrollBarEnabled(true);
          webView.setHorizontalScrollBarEnabled(true);
          webView.getSettings().setJavaScriptEnabled(true);
          webView.getSettings().setBuiltInZoomControls(true);
  
          webView.setWebViewClient(new WebViewClient() {
  
              @SuppressWarnings("deprecation")
              @Override
              public boolean shouldOverrideUrlLoading(WebView view, String url) {
                  //return true load with system-default-browser or other browsers, false with your webView
  
                  pref = getSharedPreferences("app", Context.MODE_PRIVATE);
                  final String acToken = pref.getString("token", "DEFAULT");
  
  
                  HashMap&lt;String, String&gt; headerMap = new HashMap&lt;&gt;();
                  //put all headers in this header map
                  headerMap.put("Authorization", acToken);
  
  
                  view.loadUrl(url, headerMap);
  
  
                  return false;
              }
  
              @Override
              public void onLoadResource(WebView view, String url) {
                  super.onLoadResource(view, url);
              }
  
              @Override
              public void onPageStarted(WebView view, String url, Bitmap favicon) {
                  super.onPageStarted(view, url, favicon);
              }
          });
  
          webView.loadUrl("myurl");
  
      }
  
  
      @Override
      protected void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
          webView.saveState(outState);
      }
  
      @Override
      protected void onRestoreInstanceState(Bundle savedInstanceState) {
          super.onRestoreInstanceState(savedInstanceState);
          webView.restoreState(savedInstanceState);
      }
  
  
      public WebViewClient getWebViewClientWithCustomHeader(){
  
          pref = getSharedPreferences("app", Context.MODE_PRIVATE);
          final String acToken = pref.getString("token", "DEFAULT");
  
          return new WebViewClient() {
  
              @SuppressWarnings("deprecation")
              public WebResourceResponse shouldInterceptRequest(WebView view, String url){
                  try {
                      OkHttpClient okHttpClient = new OkHttpClient();
                      Request request = new Request.Builder().url(url).addHeader("Authorization" , "Bearer " + acToken)
                              .build();
  
                      Response response = okHttpClient.newCall(request).execute();
  
                      return new WebResourceResponse(response.header("content-type", response.body().contentType().type()), // You can set something other as default content-type
                              response.header("content-encoding", "utf-8"),  // Again, you can set another encoding as default
                              response.body().byteStream());
  
  
                  } catch (ClientProtocolException e) {
                      //return null to tell WebView we failed to fetch it WebView should try again.
                      return null;
                  } catch (IOException e) {
                      e.printStackTrace();
                      return null;
                  }
              }
          };
      }
  }
  </code></pre>
  
  <p>Any help is appreciated</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Now I am able to get the server response but in plain html, not rendering the css properly, don't know why.</p>
  
  <p>Here is the new code,</p>
  
  <pre><code>    public class TableViewTest extends AppCompatActivity {
  
      ScrollView scrollView;
      WebView webView;
      SharedPreferences pref;
  
      @SuppressLint("SetJavaScriptEnabled")
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_table_view_test);
  
          //final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...", true);
  
          String url = "myurl";
          pref = getSharedPreferences("app", Context.MODE_PRIVATE);
  
  
          webView = (WebView) findViewById(R.id.webView1Id);
  
          webView.setVerticalScrollBarEnabled(true);
          webView.setHorizontalScrollBarEnabled(true);
          webView.getSettings().setJavaScriptEnabled(true);
          webView.getSettings().setBuiltInZoomControls(true);
          webView.setWebViewClient(wvc);
  
          webView.loadUrl("myurl");
  
      }
  
  
      @Override
      protected void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
          webView.saveState(outState);
      }
  
      @Override
      protected void onRestoreInstanceState(Bundle savedInstanceState) {
          super.onRestoreInstanceState(savedInstanceState);
          webView.restoreState(savedInstanceState);
      }
  
  
      public WebViewClient wvc =  new WebViewClient() {
  
              @SuppressWarnings("deprecation")
              public WebResourceResponse shouldInterceptRequest(WebView view, String url){
                  try {
                      final String acToken = pref.getString("token", "DEFAULT");
  
                      OkHttpClient okHttpClient = new OkHttpClient();
                      Request request = new Request.Builder().url(url).addHeader("Authorization" , "Bearer " + acToken)
                              .build();
  
                      Response response = okHttpClient.newCall(request).execute();
  
                      return new WebResourceResponse(response.header("content-type", response.body().contentType().type()), // You can set something other as default content-type
                              response.header("content-encoding", "utf-8"),  // Again, you can set another encoding as default
                              response.body().byteStream());
  
  
                  } catch (ClientProtocolException e) {
                      //return null to tell WebView we failed to fetch it WebView should try again.
                      return null;
                  } catch (IOException e) {
                      e.printStackTrace();
                      return null;
                  }
              }
          };
  }
  </code></pre>
  
  <p>And found this in stacktrace, nothing more in stacktrace</p>
  
  <pre><code> E/DataReductionProxySettingListener: No DRP key due to exception:java.lang.ClassNotFoundException: com.android.webview.chromium.Drp
  </code></pre>