Android WebView WebViewClient (웹뷰에서 일어나는 요청, 상태, 에러 등 다양한 상황) 재정의 사용

 WebViewClient를 통해 웹뷰에서 일어나는 요청, 상태, 에러 등 다양한 상황에서의 콜백을 조작할 수 있습니다. 

다양한 메소드를 제공하고 있습니다만 대표적으로 사용되는 몇 가지 메소드만 살펴보도록 하겠습니다. 

전체 메소드에 대해서는 developer 사이트에서 확인하실 수 있습니다.


1. shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?)

- 현재 웹뷰에 로드될 URL에 대한 컨트롤을 할 수 있는 메소드


2. onPageStarted(view: WebView?, url: String?, favicon: Bitmap?)

- page loading을 시작했을 때 호출되는 콜백 메소드


3. onPageFinished(view: WebView?, url: String?)

- page loading을 끝냈을 때 호출되는 콜백 메소드


4. onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?)

- request 에 대해 에러가 발생했을 때 호출되는 콜백 메소드. error 변수에 에러에 대한 정보가 담겨져있음


5. onReceivedHttpError(view: WebView?, request: WebResourceRequest?, errorResponse: WebResourceResponse?)

- 웹서버의 http 에러 발생시 호출되는 콜백 메서드


6. onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?)

- 웹뷰 SSL 오류관련 핸들러 콜백 메소드



override fun onReceivedError(view: WebView, request: WebResourceRequest, error: WebResourceError) { super.onReceivedError(view, request, error) Log.e("onReceivedError errorCode : ${error.errorCode}, description : ${error.description}, failingUrl : ${request.url}") val errorCode = error.errorCode //CONNECTION, TIMED_OUT, PROXY 경우 네트워크 에러페이지 노출 //errorCode : -5, description : net::ERROR_PROXY_AUTHENTICATION //errorCode : -6, description : net::ERR_CONNECTION_CLOSED //errorCode : -6, description : net::ERR_CONNECTION_ABORTED, ERR_CONNECTION_FAILED //errorCode : -6, description : net::ERR_CONNECTION_RESET, ERR_CONNECTION_REFUSED //errorCode : -6, description : net::ERR_SOCKET_NOT_CONNECTED //errorCode : -8, description : net::ERR_CONNECTION_TIMED_OUT, net::ERR_TIMED_OUT

    when (errorCode) {

ERROR_AUTHENTICATION -> {} ERROR_BAD_URL -> {} ERROR_CONNECT -> { //errorCode : -6, description : net::ERR_CONNECTION_CLOSED //errorCode : -6, description : net::ERR_CONNECTION_ABORTED //errorCode : -6, description : net::ERR_CONNECTION_FAILED //errorCode : -6, description : net::ERR_CONNECTION_RESET //errorCode : -6, description : net::ERR_CONNECTION_REFUSED //errorCode : -6, description : net::ERR_SOCKET_NOT_CONNECTED } ERROR_FAILED_SSL_HANDSHAKE -> {} ERROR_FILE -> {} ERROR_FILE_NOT_FOUND -> {} ERROR_HOST_LOOKUP -> {} ERROR_IO -> {} ERROR_IO -> {} ERROR_PROXY_AUTHENTICATION -> { //errorCode : -5, description : net::ERROR_PROXY_AUTHENTICATION } ERROR_REDIRECT_LOOP -> {} ERROR_TIMEOUT -> { //errorCode : -8, description : net::ERR_CONNECTION_TIMED_OUT //errorCode : -8, description : net::ERR_TIMED_OUT } ERROR_TOO_MANY_REQUESTS -> {} ERROR_UNKNOWN -> {} ERROR_UNSUPPORTED_AUTH_SCHEME -> {} ERROR_UNSUPPORTED_SCHEME -> {} }

}


override fun onReceivedHttpError( view: WebView?, request: WebResourceRequest?, errorResponse: WebResourceResponse?) { super.onReceivedHttpError(view, request, errorResponse) Log.d("onReceivedHttpError() url : ${view?.url}, statusCode : ${errorResponse?.statusCode}") // HTTP STATUS CODE 503 (WAS 죽음) // HTTP STATUS CODE 502 (Bad Gateway) try { var statusCode = errorResponse?.statusCode if (statusCode == 503 || statusCode == 502) { //네트워크 에러페이지 노출 } } catch (e: Exception) {} }

댓글

이 블로그의 인기 게시물

아이랑스토리 어플리케이션 개인정보처리방침

Jetpack Compose Navigation 정리

Jetpack Compose 기초정리