gpt4 book ai didi

android - 即使服务器发回错误,WebView 也未收到错误

转载 作者:行者123 更新时间:2023-11-29 22:26:47 25 4
gpt4 key购买 nike

我尝试访问的网页发回了 403 错误。我期待 WebViewClient 收到错误,但事实并非如此。它不应该出错吗?

engine.setWebViewClient(new WebViewClient() {
boolean error = false;
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {

//IT DOESN'T COME HERE even IF SERVER SENDS BACK 403 ERROR
}
});

它看起来像,我必须使用 HttpRequest 读取响应并解析响应以检查发生了什么。还有其他方法吗?我可以从 WebView 中检查 header 吗?

最佳答案

我必须使用两个请求来实现该要求。首先请求检查凭据是否正常并且没有错误(使用 HttpURLConnection)。只有当响应正常时,我才会加载 webview。

protected Void doInBackground(final String... url) {
boolean failed = false;
int error = 0;

try {
URL urlToLoad = new URL(url[0]);
HttpURLConnection conn = (HttpURLConnection) urlToLoad
.openConnection();
int response = conn.getResponseCode();
if (response != HttpURLConnection.HTTP_OK) {
error = response;
failed = true;
}
} catch (Exception e) {
e.printStackTrace();
}
if (!failed) {
webView.loadUrl(url[0]);
} else {
handler.post(hideProgressRunnable);
connecting = false;
showError(ErrorMap.getErrorForHttpCode(error));
}
return null;
}

关于android - 即使服务器发回错误,WebView 也未收到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5682024/

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