I'm developing an Android app that loads a website in a WebView, but sometimes the website returns HTTP code 500.
My question is: is there any way to get the HTTP status code from a WebView with a listener or with another class??
I tried to implement an WebViewClient, but I couldn't get the HTTP status code which WebView received.
It looks as though this is possible via a new callback in the Android M API (https://code.google.com/p/android/issues/detail?id=82069#c7).
Unfortunately, this will most likely not available in pre-Android M devices.
At this time you cannot get normal HTTP response code.
But as solution, if is possible to you to modify webapp server side, to use following:
On the server create some JavaScript function, let's say,
riseHttpError
.On android side use JavaScript interface and when you need to tell android to handle http error, just call
Android.riseHttpError()
on server.Android will handles this function and you will be able to do required actions on android side.
In my solution were required to get errors. You can send any code you want. :)
Of course, this is just another variation how to do it. So, probably there is others, much better solutions.
But if you can modify server side, I think, this will be better to do double request using
URLHandler
.Actually it's not too bad to check HTTP status to see if content is available to load into a webview. Assumed: You've already set your webview and you have the string for your target URL then...
Elsewhere, in WebViewClient, dismiss the progress dialog
I don't think it is possible to get status code in easy way(if it's at all possible) from webView.
My idea is to use onReceivedError() method from WebViewClient(as you said) with defined errors in WebViewClient (full list of errors is available here: http://developer.android.com/reference/android/webkit/WebViewClient.html) and assume that for instance 504 status code is equals to WebViewClient.ERROR_TIMEOUT etc.
You should use this after the on Page finished
It's not possible (as of the time i'm writing this). The docs for
onReceiveError()
are ambiguous at best, but it you look at this issue,It's clear that HTTP status codes won't be reported through that mechanism. How it's possible that the developers wrote
WebView
with no way to retrieve the HTTP status code really blows my mind.