加载https和http 报错 Failed to validate the certificate chain,
flutter_inappwebview加载http请求,非https时由于安全性或者其他问题。默认是无法加载的。需要一些额外的设置。
原因是:从安卓9开始,因为cleartext属性默认是不支持的,需要手动设置为可用状态(官网的描述)。
1.解决方法:
在AndroidManifest.xml中添加
android:usesCleartextTraffic="true",别写到activity中去了
添加完成后是这样的:
<application
android:label="------"
android:usesCleartextTraffic="true"
android:icon="@mipmap/logo">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
</activity>
</application>
- 加载报错: handshake failed; returned -1, ssl error code 1, net_error -202
InAppWebView(
initialUrl: _url,
onReceivedServerTrustAuthRequest: (controller, challenge) async {
//解决 handshake failed问题
return ServerTrustAuthResponse(
action: ServerTrustAuthResponseAction.PROCEED);
},
onLoadStop: (InAppWebViewController controller, String url) {
loadingDismiss();
print("网页 onLoadStop--》");
},
)
3.加载之前 在main中设置 flutter_inappwebview配置
Future<void> _initWebView() async {
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isAndroid) {
await AndroidInAppWebViewController.setWebContentsDebuggingEnabled(true);
var swAvailable = await AndroidWebViewFeature.isFeatureSupported(
AndroidWebViewFeature.SERVICE_WORKER_BASIC_USAGE);
var swInterceptAvailable = await AndroidWebViewFeature.isFeatureSupported(
AndroidWebViewFeature.SERVICE_WORKER_SHOULD_INTERCEPT_REQUEST);
if (swAvailable && swInterceptAvailable) {
AndroidServiceWorkerController serviceWorkerController =
AndroidServiceWorkerController.instance();
serviceWorkerController.serviceWorkerClient = AndroidServiceWorkerClient(
shouldInterceptRequest: (request) async {
print(request);
return null;
},
);
}
}
}
网友评论