简体   繁体   English

当网页回应错误状态时,未呼叫Android Webview onReceivedError()

[英]Android webview onReceivedError() not called when webpage responds with error status

onReceivedError() is called if the webview tries to load some non-existent page, but it is not called if the server/website chooses to respond with an error status, such as status 500. 如果Web视图尝试加载一些不存在的页面,则调用onReceivedError(),但是如果服务器/网站选择以错误状态(例如状态500)进行响应,则不会调用onReceivedError()。

# Django returning status 500
return HttpResponseServerError("returned status 500")

This response appears in the regular Chrome browser with status 500. However, the webview just loads it, never calling onReceivedError(). 此响应会在状态为500的常规Chrome浏览器中显示。但是,网络视图只会加载它,而不会调用onReceivedError()。

How can the app/webview tell if the server is sending back an error status? app / webview如何判断服务器是否发回错误状态? I need to handle it like any error. 我需要像处理任何错误一样处理它。

Thanks! 谢谢!

Have a look here Android Developer WebViewClient. 在这里查看Android Developer WebViewClient。

You can read here on which errors onReivedError is responding. 您可以在此处阅读onReivedError响应的错误。 HTML error 500 (internal server error is not within it). HTML错误500(内部服务器错误不在其中)。 Further on ReceivedError is deprecated since version 23. You can read this all in the former link. 自版本23开始,不建议再使用ReceivedError。您可以在以前的链接中阅读全部内容。

I was looking around very quickly and found more questions about how to get the http errors, mostly the answers were negative. 我很快浏览了一下,发现了更多有关如何获得http错误的问题,大多数答案都是负面的。

So maybe you can use a trick by testing the website with an HttpURLConnection and use the method getResponseCode to get the status of your server. 因此,也许您可​​以通过使用HttpURLConnection测试网站来使用技巧,并使用getResponseCode方法获取服务器的状态。 You can for example put a small file on the server and then you try to get this small file with HttpURLConnection. 例如,您可以在服务器上放置一个小文件,然后尝试使用HttpURLConnection获取此小文件。 If there is an internal server error you will receive a 500 error as response. 如果发生内部服务器错误,您将收到500错误作为响应。 And then you can handle things like you want. 然后,您可以处理所需的事情。

I know this is not the most elegant answer, but as I said maybe you can use some kind of trick. 我知道这不是最优雅的答案,但正如我所说,也许您可​​以使用某种技巧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM