Kaynağa Gözat

优化H5错误页控制逻辑

Pq 4 ay önce
ebeveyn
işleme
e748f410ad

+ 11 - 3
student/src/main/java/com/cooleshow/student/ui/web/HtmlActivity.java

@@ -178,6 +178,7 @@ public class HtmlActivity extends BaseActivity<ActivityHtml1Binding> implements
     private boolean isNeedResetScreenOrientation = true;
     private boolean isResetOrientation = false;
     private boolean isLoadError = false;
+    private boolean isLoadCompleted = false;
     private DownloadHelper.OnEventListener saveFileListener;
 
     private DownloadHelper.OnEventListener mEventListener;
@@ -1190,6 +1191,7 @@ public class HtmlActivity extends BaseActivity<ActivityHtml1Binding> implements
         public void onPageStarted(WebView view, String url, Bitmap favicon) {
             super.onPageStarted(view, url, favicon);
             isLoadError = false;
+            isLoadCompleted = false;
         }
 
         //页面载入结束时调用
@@ -1198,6 +1200,7 @@ public class HtmlActivity extends BaseActivity<ActivityHtml1Binding> implements
             super.onPageFinished(view, url);
             if (!isLoadError) {
                 showWebView();
+                isLoadCompleted =true;
             }
             if (!TextUtils.isEmpty(authorization)) {
                 String js = "window.localStorage.setItem('Authorization','" + authorization + "');";
@@ -1255,15 +1258,20 @@ public class HtmlActivity extends BaseActivity<ActivityHtml1Binding> implements
         //处理报错信息
         @Override
         public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
-            super.onReceivedError(view, request, error);
+//            super.onReceivedError(view, request, error);
             if (error != null) {
                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                     LOG.i("onReceivedError:" + error.getErrorCode());
                     LOG.i("onReceivedError:" + error.getDescription());
-                    if (error.getErrorCode() == WebViewClient.ERROR_HOST_LOOKUP || error.getErrorCode() == WebViewClient.ERROR_CONNECT) {
+                    int errorCode = error.getErrorCode();
+                    if (errorCode== WebViewClient.ERROR_HOST_LOOKUP || errorCode == WebViewClient.ERROR_CONNECT || errorCode == WebViewClient.ERROR_PROXY_AUTHENTICATION) {
+                        if (isLoadCompleted) {
+                            //20240827同步加载完后的error不需要显示 错误页
+                            return;
+                        }
                         showLoadErrorView();
                     } else {
-                        if (error.getErrorCode() == WebViewClient.ERROR_UNKNOWN) {
+                        if(error.getErrorCode() == WebViewClient.ERROR_UNKNOWN) {
                             return;
                         }
                         String errorMsg = "url:" + request.getUrl().toString() + " " + error.getDescription();

+ 10 - 2
teacher/src/main/java/com/cooleshow/teacher/ui/web/HtmlActivity.java

@@ -181,6 +181,7 @@ public class HtmlActivity extends BaseActivity<ActivityHtml1Binding> implements
     private boolean isResetOrientation = false;
 
     private boolean isLoadError = false;
+    private boolean isLoadCompleted = false;
     private DownloadHelper.OnEventListener mEventListener;
     private DownloadHelper.OnEventListener saveFileListener;
 
@@ -1195,6 +1196,7 @@ public class HtmlActivity extends BaseActivity<ActivityHtml1Binding> implements
         public void onPageStarted(WebView view, String url, Bitmap favicon) {
             super.onPageStarted(view, url, favicon);
             isLoadError = false;
+            isLoadCompleted = false;
         }
 
         //页面载入结束时调用
@@ -1202,6 +1204,7 @@ public class HtmlActivity extends BaseActivity<ActivityHtml1Binding> implements
         public void onPageFinished(WebView view, String url) {
             super.onPageFinished(view, url);
             if (!isLoadError) {
+                isLoadCompleted =true;
                 showWebView();
             }
             if (!TextUtils.isEmpty(authorization)) {
@@ -1265,9 +1268,14 @@ public class HtmlActivity extends BaseActivity<ActivityHtml1Binding> implements
                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                     LOG.i("onReceivedError:" + error.getErrorCode());
                     LOG.i("onReceivedError:" + error.getDescription());
-                    if (error.getErrorCode() == WebViewClient.ERROR_HOST_LOOKUP || error.getErrorCode() == WebViewClient.ERROR_CONNECT) {
+                    int errorCode = error.getErrorCode();
+                    if (errorCode== WebViewClient.ERROR_HOST_LOOKUP || errorCode == WebViewClient.ERROR_CONNECT || errorCode == WebViewClient.ERROR_PROXY_AUTHENTICATION) {
+                        if (isLoadCompleted) {
+                            //20240827同步加载完后的error不需要显示 错误页
+                            return;
+                        }
                         showLoadErrorView();
-                    } else {
+                    }else {
                         if(error.getErrorCode() == WebViewClient.ERROR_UNKNOWN){
                             return;
                         }