Skip to content

Commit d4e107e

Browse files
committed
fix login issue on android 16
1 parent 5a91428 commit d4e107e

2 files changed

Lines changed: 90 additions & 39 deletions

File tree

app/src/main/java/com/zfdang/zsmth_android/WebviewLoginActivity.java

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.os.Bundle;
44
import android.util.Log;
5+
import android.webkit.ConsoleMessage;
56
import android.webkit.RenderProcessGoneDetail;
67
import android.webkit.WebChromeClient;
78
import android.webkit.WebSettings;
@@ -30,42 +31,54 @@ protected void onCreate(Bundle savedInstanceState) {
3031

3132
mWebView = (WebView) findViewById(R.id.webview_login);
3233

33-
WebSettings webSettings = mWebView.getSettings();
34-
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
35-
webSettings.setGeolocationEnabled(false);
36-
webSettings.setSaveFormData(false);
37-
webSettings.setSavePassword(false);
38-
webSettings.setJavaScriptEnabled(true); ///------- 设置javascript 可用
39-
40-
// https://stackoverflow.com/questions/9602124/enable-horizontal-scrolling-in-a-webview
41-
webSettings.setLoadWithOverviewMode(true);
42-
webSettings.setUseWideViewPort(true);
34+
try {
35+
WebSettings webSettings = mWebView.getSettings();
36+
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
37+
webSettings.setGeolocationEnabled(false);
38+
webSettings.setSaveFormData(false);
39+
webSettings.setSavePassword(false);
40+
webSettings.setJavaScriptEnabled(true); ///------- 设置javascript 可用
41+
42+
// https://stackoverflow.com/questions/9602124/enable-horizontal-scrolling-in-a-webview
43+
webSettings.setLoadWithOverviewMode(true);
44+
webSettings.setUseWideViewPort(true);
4345
// mWebView.setInitialScale(120);
4446
// mWebView.getSettings().setTextZoom(200);
4547

46-
//支持屏幕缩放
47-
webSettings.setSupportZoom(true);
48-
webSettings.setBuiltInZoomControls(true);
48+
//支持屏幕缩放
49+
webSettings.setSupportZoom(true);
50+
webSettings.setBuiltInZoomControls(true);
4951

50-
// Android 16兼容性修复:启用必要的WebView功能
51-
// Fix for Android 16: Enable necessary WebView features
52-
webSettings.setDomStorageEnabled(true); // 启用DOM存储,现代网页必需
53-
webSettings.setDatabaseEnabled(true); // 启用数据库存储
52+
// Android 16兼容性修复:启用必要的WebView功能
53+
// Fix for Android 16: Enable necessary WebView features
54+
webSettings.setDomStorageEnabled(true); // 启用DOM存储,现代网页必需
55+
webSettings.setDatabaseEnabled(true); // 启用数据库存储
5456

55-
// 允许混合内容 (需要 API 21+)
56-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
57-
webSettings.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
58-
}
57+
// 允许混合内容 (需要 API 21+)
58+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
59+
webSettings.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
60+
}
5961

60-
// Android 16兼容性修复:确保WebView渲染器不会因为错误而白屏
61-
// Fix for Android 16: Ensure WebView renderer doesn't crash
62-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
63-
webSettings.setSafeBrowsingEnabled(true);
64-
}
62+
// Android 16兼容性修复:确保WebView渲染器不会因为错误而白屏
63+
// Fix for Android 16: Ensure WebView renderer doesn't crash
64+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
65+
webSettings.setSafeBrowsingEnabled(true);
66+
}
67+
68+
// Ensure user agent has a modern token to avoid some sites returning mobile-empty pages
69+
String ua = webSettings.getUserAgentString();
70+
if (ua == null || !ua.contains("Mobile")) {
71+
webSettings.setUserAgentString(ua + " Mobile-SMTH-App");
72+
}
73+
74+
// Only set hardware layer on API 17+ to be safe
75+
if (android.os.Build.VERSION.SDK_INT >= 17) {
76+
mWebView.setLayerType(android.view.View.LAYER_TYPE_HARDWARE, null);
77+
}
6578

66-
// 启用硬件加速以提高性能(Android 16推荐)
67-
// Enable hardware acceleration for better performance (recommended for Android 16)
68-
mWebView.setLayerType(android.view.View.LAYER_TYPE_HARDWARE, null);
79+
} catch (Throwable t) {
80+
Log.e(TAG, "Exception while configuring WebView settings", t);
81+
}
6982

7083
mWebView.setWebChromeClient(new WebChromeClient()
7184
{
@@ -75,6 +88,12 @@ public void onProgressChanged(WebView view, int newProgress)
7588
super.onProgressChanged(view, newProgress);
7689
view.requestFocus();
7790
}
91+
92+
@Override
93+
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
94+
Log.i(TAG, "Console: " + consoleMessage.message() + " -- " + consoleMessage.sourceId() + ":" + consoleMessage.lineNumber());
95+
return super.onConsoleMessage(consoleMessage);
96+
}
7897
});
7998

8099
// 创建自定义的 WebViewClient 并添加渲染器崩溃处理
@@ -102,7 +121,16 @@ public boolean onRenderProcessGone(WebView view, RenderProcessGoneDetail detail)
102121
};
103122

104123
mWebView.setWebViewClient(webViewClient);
105-
mWebView.loadUrl(url);
124+
125+
try {
126+
mWebView.loadUrl(url);
127+
} catch (Throwable t) {
128+
Log.e(TAG, "Exception while loading URL", t);
129+
// Show a simple fallback message instead of a white screen
130+
if (mWebView != null) {
131+
mWebView.loadData("<html><body><h3>加载失败,请检查网络或重试</h3></body></html>", "text/html", "UTF-8");
132+
}
133+
}
106134
}
107135

108136
@Override

app/src/main/java/com/zfdang/zsmth_android/WebviewLoginClient.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import android.content.Intent;
55
import android.os.Build;
66
import android.util.Log;
7-
import android.webkit.ValueCallback;
7+
import android.webkit.SslErrorHandler;
8+
import android.net.http.SslError;
89
import android.webkit.WebResourceError;
910
import android.webkit.WebResourceRequest;
1011
import android.webkit.WebResourceResponse;
@@ -18,8 +19,8 @@
1819
public class WebviewLoginClient extends WebViewClient {
1920

2021
private static final String TAG = "WebviewLoginClient";
21-
private String username;
22-
private String password;
22+
private final String username;
23+
private final String password;
2324

2425
Activity activity;
2526

@@ -49,7 +50,7 @@ public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
4950

5051
public void onPageFinished(WebView view, String url) {
5152
// Log.d(TAG, "onPageFinished" + url);
52-
if (url.equals("https://m.newsmth.net/index")) {
53+
if (url != null && url.contains("m.newsmth.net/index")) {
5354
// login page, input id and passwd automatically
5455
final String js = "javascript: " +
5556
"var ids = document.getElementsByName('id');" +
@@ -59,11 +60,7 @@ public void onPageFinished(WebView view, String url) {
5960
"document.getElementById('TencentCaptcha').click();";
6061

6162
if (Build.VERSION.SDK_INT >= 19) {
62-
view.evaluateJavascript(js, new ValueCallback<String>() {
63-
@Override
64-
public void onReceiveValue(String s) {
65-
}
66-
});
63+
view.evaluateJavascript(js, s -> { });
6764
} else {
6865
view.loadUrl(js);
6966
}
@@ -84,4 +81,30 @@ public void onReceivedError(WebView view, int errorCode, String description, Str
8481
super.onReceivedError(view, errorCode, description, failingUrl);
8582
Log.e(TAG, "WebView error: " + description + " (code: " + errorCode + ") for URL: " + failingUrl);
8683
}
84+
85+
// Handle SSL errors: some devices / WebView versions may fail TLS handshake for certain cert chains.
86+
// For better UX, allow proceeding for our known host (m.newsmth.net) while keeping other hosts safe.
87+
@Override
88+
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
89+
try {
90+
String host = (error != null && error.getUrl() != null) ? error.getUrl() : null;
91+
if (host == null && view != null && view.getUrl() != null) {
92+
host = view.getUrl();
93+
}
94+
Log.e(TAG, "onReceivedSslError for host: " + host + ", primaryError=" + (error != null ? error.getPrimaryError() : "null"));
95+
// Be conservative: only proceed automatically for our own domain to avoid security issues.
96+
if (host != null && host.contains("newsmth.net")) {
97+
handler.proceed();
98+
Log.w(TAG, "Proceeding SSL error for trusted host: " + host);
99+
} else {
100+
handler.cancel();
101+
Log.w(TAG, "Cancelled SSL for host: " + host);
102+
}
103+
} catch (Exception e) {
104+
Log.e(TAG, "Exception in onReceivedSslError", e);
105+
try {
106+
handler.cancel();
107+
} catch (Exception ignored) {}
108+
}
109+
}
87110
}

0 commit comments

Comments
 (0)