问题
I am using delight-im/Android-AdvancedWebView for loading a url with extra headers (user auth token), but the headers are sending only with the initial request, that is the first url the webview is loading. But when I click another link in that url and try to POST some value the header is not passed along. How it can be extended to all GET and POST requests with the url's associated with base url ? Please mention a way to do this. Here is my code sample for loading the url with webview.
public class TableViewTest extends AppCompatActivity implements AdvancedWebView.Listener {
SharedPreferences pref;
boolean preventCaching = true;
private static final String URL = "my-url";
private AdvancedWebView mWebView;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table_view_test);
pref = getSharedPreferences("LoginActivity", Context.MODE_PRIVATE);
final String acToken = pref.getString("token", "DEFAULT");
//used this method to add headers with every request, not working
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "Bearer " + acToken);
//mWebView.loadUrl(your url, headers);
//webView = (WebView) findViewById(R.id.webView1Id);
mWebView = (AdvancedWebView) findViewById(R.id.webview);
mWebView.setListener(this, this);
mWebView.setGeolocationEnabled(false);
mWebView.setMixedContentAllowed(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
//mWebView.setCookiesEnabled(true);
////////////////////////////////////////////////////////////////////////////////////////////
mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setAppCacheEnabled(false);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.clearCache(true);
////////////////////////////////////////////////////////////////////////////////
//mWebView.setThirdPartyCookiesEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE))
{ WebView.setWebContentsDebuggingEnabled(true); }
}
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
Toast.makeText(TableViewTest.this, "Finished loading", Toast.LENGTH_SHORT).show();
/////////////////////////////////////////////////////////////////////////////////////////////
mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setAppCacheEnabled(false);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.clearCache(true);
////////////////////////////////////////////////////////////////////////
}
});
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
Toast.makeText(TableViewTest.this, title, Toast.LENGTH_SHORT).show();
}
});
//default method for adding header for initial method in AdvancedWebview
mWebView.addHttpHeader("Authorization", "Bearer " + acToken);
mWebView.loadUrl(URL, headers);
// webView.getSettings().setJavaScriptEnabled(true);
// webView.getSettings().setBuiltInZoomControls(true);
// webView.getSettings().setDomStorageEnabled(true);
// webView.getSettings().setUseWideViewPort(true);
}
@SuppressLint("NewApi")
@Override
protected void onResume() {
super.onResume();
mWebView.onResume();
// ...
}
@SuppressLint("NewApi")
@Override
protected void onPause() {
mWebView.onPause();
// ...
super.onPause();
}
@Override
protected void onDestroy() {
mWebView.onDestroy();
// ...
super.onDestroy();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
mWebView.onActivityResult(requestCode, resultCode, intent);
// ...
}
@Override
public void onBackPressed() {
if (!mWebView.onBackPressed()) { return; }
// ...
super.onBackPressed();
}
@Override
public void onPageStarted(String url, Bitmap favicon) {
mWebView.setVisibility(View.INVISIBLE);
}
@Override
public void onPageFinished(String url) {
mWebView.setVisibility(View.VISIBLE);
mWebView.clearCache(true);
mWebView.getSettings().setAppCacheEnabled(false);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
}
@Override
public void onPageError(int errorCode, String description, String failingUrl) {
Toast.makeText(TableViewTest.this, "onPageError(errorCode = "+errorCode+", description = "+description+", failingUrl = "+failingUrl+")", Toast.LENGTH_SHORT).show();
}
@Override
public void onDownloadRequested(String url, String suggestedFilename, String mimeType, long contentLength, String contentDisposition, String userAgent) {
Toast.makeText(TableViewTest.this, "onDownloadRequested(url = "+url+", suggestedFilename = "+suggestedFilename+", mimeType = "+mimeType+", contentLength = "+contentLength+", contentDisposition = "+contentDisposition+", userAgent = "+userAgent+")", Toast.LENGTH_LONG).show();
/*if (AdvancedWebView.handleDownload(this, url, suggestedFilename)) {
// download successfully handled
}
else {
// download couldn't be handled because user has disabled download manager app on the device
}*/
}
@Override
public void onExternalPageRequest(String url) {
Toast.makeText(TableViewTest.this, "onExternalPageRequest(url = "+url+")", Toast.LENGTH_SHORT).show();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
webView.saveState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
webView.restoreState(savedInstanceState);
}
}
回答1:
You can add the following code:
mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url, getExtraHeaders());
return true;
}
});
where getExtraHeaders() returns a Map<String, String> containing the additional headers to be used in the HTTP request for this URL.
Also, please note that addHttpHeader has been fixed now and sends custom headers with every GET request.
来源:https://stackoverflow.com/questions/41630491/delight-im-advanced-webview-send-custom-headers-with-every-get-post-request