我想从webView向服务器发送自定义标头。
我的解决方案适用于加载的第一个页面,但如果用户点击webView中的链接,则无法正常工作。
遵循代码。
我使用我的所有者类扩展了WebView并覆盖了一些方法:
public class SMWebViewClient extends WebViewClient {
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
android.util.Log.i("MyWebViewClient", "attempting to load resource: " + request.getUrl());
if(view.getClass().equals(SMWebView.class)){
SMWebView smWebView = (SMWebView) view;
request.getRequestHeaders().putAll(smWebView.getHttpHeaders());
request.getRequestHeaders().put("HIDE_MENU","true");
}
return super.shouldInterceptRequest(view,request);
}
}
我还扩展了WebViewClient
public class MainActivity extends AppCompatActivity implements DrawerMenuItem.DrawerCallBack{
@BindView(R.id.main_webview) SMWebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
webView.setWebViewClient(new SMWebViewClient());
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.loadUrl(Constant.baseUrl + Constant.homeUrl);
}
...
}
我在MainActivity中使用它们:
let mix = require('laravel-mix');
/*
|--------------------------------------------------------------------------
| Mix Asset Management
|--------------------------------------------------------------------------
|
| Mix provides a clean, fluent API for defining some Webpack build steps
| for your Laravel application. By default, we are compiling the Sass
| file for the application as well as bundling up all the JS files.
|
*/
mix.js(['resources/assets/js/app
.js'], 'public/js').version()
.sass('resources/assets/sass/app.scss', 'public/css').version();
即使用户点击webView上的链接,如何将自定义标题发送到每个请求?
答案 0 :(得分:1)
我解决了在WebViewClient中添加这个方法的问题:
@Override
public boolean shouldOverrideUrlLoading(WebView view,WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String uri) {
view.loadUrl(uri);
return true;
}