gpt4 book ai didi

android - Delight-im 高级 webview 在每个 GET 和 POST 请求中发送自定义 header

转载 作者:太空狗 更新时间:2023-10-29 14:44:56 46 4
gpt4 key购买 nike

我正在使用 delight-im/Android-AdvancedWebView 加载带有额外 header (用户身份验证 token )的 url,但 header 仅随初始请求一起发送,这是 webview 加载的第一个 url。但是当我单击该 url 中的另一个链接并尝试 POST 一些值时,标题不会传递。如何将其扩展到所有 GET 和 POST 请求,其中 url 与基本 url 相关联?请提及一种方法来做到这一点。这是我使用 webview 加载 url 的代码示例。

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);
}
}

最佳答案

您可以添加以下代码:

mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url, getExtraHeaders());
return true;
}
});

哪里getExtraHeaders()返回 Map<String, String>包含要在对此 URL 的 HTTP 请求中使用的附加 header 。

另外,请注意 addHttpHeader现已修复并随每个 GET 请求发送自定义 header 。

关于android - Delight-im 高级 webview 在每个 GET 和 POST 请求中发送自定义 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41630491/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com