我正在尝试使用WebView开发Android浏览器应用程序,使用户能够从自定义协议访问内容.自定义协议可能是foobar://
我想拦截对此自定义协议的所有请求.这意味着:
我需要能够将这些操作的结果交还给WebView.
可以使用shouldInterceptRequest(可从API级别11获得)处理GET请求.
现在我的问题是:我如何能够接受和处理POST请求?
android url-interception protocol-handler android-webview webviewclient
我想在WebView中加载网页但删除部分网页.所以,我创建了一个自定义WebViewClient.并且,在onPageFinished()中,我做了一些javascript来删除一些元素.然后,我使WebView可见.
但是,当我运行它时,它将视图设置为可见,然后我看到要删除的元素.就好像JS在后台运行非常慢.它会创建一个糟糕的观看体验,因为它会闪烁整个页面,然后闪烁所需的部分页面.
这是我的onPageFinished()
@Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:"
+ "document.getElementsByClassName('header')[0].style.display='none';"
+ "document.getElementById('section_0').style.display='none';"
+ "document.getElementById('page-actions').style.display='none';"
+ "document.getElementsByClassName('languageSelector')[0].style.display='none';"
+ "document.getElementById('mw-mf-last-modified').style.display='none';"
+ "document.getElementById('footer').style.display='none';");
loadingView.setVisibility(View.INVISIBLE);
view.setVisibility(View.VISIBLE);
}
Run Code Online (Sandbox Code Playgroud)
有想法该怎么解决这个吗?
我onReceivedSslError在我的WebViewClient中实现了一个方法来正确处理webview中的无效https证书:
@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
final AlertDialog.Builder builder = new AlertDialog.Builder(WebActivity.this);
String message = "SSL Certificate error.";
switch (error.getPrimaryError()) {
case SslError.SSL_UNTRUSTED:
message = "The certificate authority is not trusted.";
return;
case SslError.SSL_EXPIRED:
message = "The certificate has expired.";
break;
case SslError.SSL_IDMISMATCH:
message = "The certificate Hostname mismatch.";
break;
case SslError.SSL_NOTYETVALID:
message = "The certificate is not yet valid.";
break;
}
message += " Do you want to continue anyway?";
builder.setTitle("SSL …Run Code Online (Sandbox Code Playgroud) 我有一个简单的WebViewClient用于我的WebView并且覆盖了shouldInterceptRequest :(不是实际的代码)
public class WebViewClientBook extends WebViewClient
{
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url)
{
File file = new File("pathToTheFile.jpeg");
FileInputStream inputStream = new FileInputStream(file);
return new WebResourceResponse("image/jpeg", "UTF-8", inputStream);
}
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,WebClient无法显示图像...我相信它可能与不正确的编码有关:UTF-8.
有什么建议可以作为替代方案吗?
谢谢!
我最近注意到我的应用程序偶尔有LAG.而LAG我的意思是它可能需要40秒,取决于我使用Wifi还是移动数据......
我加载页面URL,然后加载js执行:
webView = (WebView) view.findViewById(R.id.WebView);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
logDebug("Loading URL: " + url);
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return WrappingClass.this.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
logInfo("loading JavaScript to webview.");
webView.loadUrl("full-js-here");
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
logError("error code:" + errorCode);
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
WebSettings webSettings …Run Code Online (Sandbox Code Playgroud) android android-webview android-browser webviewclient android-websettings
我有一个WebView,它使用以下设置将其图像缓存到目录中:
mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
mWebView.getSettings().setAppCacheMaxSize( 8 * 1024 * 1024 ); // 8MB
mWebView.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath());
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setAppCacheEnabled(true);
Run Code Online (Sandbox Code Playgroud)
我希望能够将使用此WebView缓存的图像加载到ImageView中.
我正在查看缓存目录指向的目录使用adb shell(/data/data/com.example.webviewtest/cache),我找到了以下文件夹:
有没有办法在这些目录中找到下载的图像?
如果是,我可以将它们加载到Bitmap(在ImageView中使用)吗?
是否可以使用okHttp作为webviewclient?
即 mWebView.setWebViewClient(new OkHttpClient());
这给了我一个错误......
The method setWebViewClient(WebViewClient) in the type WebView is not applicable for the arguments (OkHttpClient)
有什么建议?
我试图了解差异b/w https://developer.android.com/reference/android/webkit/WebViewClient.html
onRecievedHttpError和onRecievedError.
对我来说,我总是在网页上的所有错误中得到这两个回调?为什么我们有两个回调呢.
我尝试在 Android WebView 中使用 CookieManager 获取 cookie 信息。然而,它所做的只是提供一个键值对 cookie,没有额外的信息。
我也尝试过这篇文章并用于connection.getHeaderFields.get("Set-Cookie");获取从服务器收到的所有 cookie。但据我所知,我没有看到我应该从服务器接收的所有 cookie。这也可以通过 CookieManger 中的某些 cookie 收到新值但未显示在Set-Cookie字段中来发现。
理想情况下,我想拦截每个响应并使用来自服务器的所有信息(如名称、值、最大年龄、安全等)检查他们的 cookie。有什么建议如何使用 Android WebView (WebViewClient) 执行此操作?
我正在使用带有 WebViewClient 的 Webview 来加载需要身份验证的 URL。我正在使用WebViewClient 的onReceivedHttpAuthRequest方法为每个请求设置身份验证。
@Override
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
handler.proceed("username", "password");
}
Run Code Online (Sandbox Code Playgroud)
这适用于只需要基本身份验证的 URL。
但是,我的应用程序中的某些 URL 需要使用请求中的一些额外标头进行登录访问。所以对于这种请求,基本的认证是不够的,服务器会抛出 401 auth 错误。
因此,对于这种情况,我需要捕获 401 错误并需要将用户导航到登录屏幕。从棉花糖开始,WebViewClient 提供了以下方法来处理此类 HTTP 错误。
@Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
super.onReceivedHttpError(view, request, errorResponse);
if(errorResponse.getStatusCode() ==401){
// Code to navigate user to Login Screen.
}
}
Run Code Online (Sandbox Code Playgroud)
但是我没有找到任何可以处理或获取此类 401 错误回调的棉花糖设备。此外,回调方法onReceivedError也不起作用。
任何帮助,将不胜感激。
webviewclient ×10
android ×9
webview ×3
caching ×1
cookies ×1
http-error ×1
https ×1
javascript ×1
okhttp ×1
ssl ×1