我正在使用delight-im / Android-AdvancedWebView来加载带有额外标头(用户身份验证令牌)的网址,但是标头只发送初始请求,这是webview加载的第一个网址。但是,当我单击该URL中的另一个链接并尝试POST一些值时,标题不会传递。如何将其扩展到所有GET和POST请求,其中url与基本URL相关联? 请提一下这样做的方法。 这是我用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);
}
}
答案 0 :(得分:0)
您可以添加以下代码:
mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url, getExtraHeaders());
return true;
}
});
其中getExtraHeaders()
返回一个Map<String, String>
,其中包含要在此URL的HTTP请求中使用的其他标头。
另外,请注意,addHttpHeader
已修复,并随每个GET请求发送自定义标头。