2

I got error code if any error in webpage by onReceivedHttpError method of setWebViewClient but when there is no error any page load success or The server callback me code such as 403 then how can i check if status code 200 or 403.

Shine Lee
  • 37
  • 1
  • 9
  • see this http://stackoverflow.com/q/11889020/6482350. As far as I know there is not a good way in pre-Android M devices. – DysaniazzZ Nov 24 '16 at 03:17

1 Answers1

-2

You could use in web view

webView.setWebViewClient(new WebViewClient() {

@SuppressWarnings("deprecation")
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
    // Handle the error
}
 @Override
 public void onPageFinished(WebView view, String url) {
     super.onPageFinished(view, url);
}
@Override
 public void onReceivedSslError(final WebView view, final SslErrorHandler handler, final SslError error) {
            // TODO Auto-generated method stub
            super.onReceivedSslError(view, handler, error);
           // handle your SSL related error here
           // handler.proceed();
        }
});
Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60
  • thx,but this is request onError...I want the normal status code such as 200. – Shine Lee Sep 29 '16 at 05:48
  • yes in onReceivedError method you are getting errorCode as well. Use it – Rahul Khurana Sep 29 '16 at 05:50
  • Even if the correct code such as 200 or 403 can be received?....ok ,I'll try it ,thx. – Shine Lee Sep 30 '16 at 03:56
  • onPageFinished method will run only when there is status code 200 – Rahul Khurana Sep 30 '16 at 04:06
  • you can apply your conditions in onPageFinished method for status code 200 – Rahul Khurana Sep 30 '16 at 04:07
  • 1
    thank u very much! one more thing : What if I want to get the server custom return code? such as 403(my server give 403 to me when user hasn't authorization.And the 403 neither in the onReceivedError() nor in other method) – Shine Lee Sep 30 '16 at 07:37
  • i edited my answer with adding onReceivedSslError method . If it helps you upto some extent please accept it as answer and upvote. – Rahul Khurana Sep 30 '16 at 08:13
  • @RahulKhurana Replying to your comment : "onPageFinished method will run only when there is status code 200". This is not true. It gets called when the page is loaded, irrespective of response. I tried with a 404 URL. In this method i got the parameter url value as "about:blank" Just in case it helps someone. – Rahul Ahuja Aug 13 '17 at 10:34
  • onReceivedError(WebView view, int errorCode, String description, String failingUrl) this method is deprecated. Replacement would be onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) wherein errorResponse.getStatusCode() will give you HTTP status code (this call is available >= 21 api) can anyone suggest what in case i want to use api prior to 21 and still get HTTP status code and also avoid using deprecated method. Thanks. – Rahul Ahuja Aug 13 '17 at 10:38