При этом мне нужно загрузить URL http://192.168.8.200: 8000 / surveyys / SLTest01? Auth = 3hZE9Zf7a в android веб-представление. Но, к сожалению, эта страница не загружается на устройство android 9 (P ie), а устройство android 5.1.1 (Lollipop) прекрасно загружает эту страницу. При загрузке URL на устройстве android p ie произошла ошибка «android webkit resource».
IntakeReadingsFragment.kt
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
binding.setLifecycleOwner(this)
myWebSettings = binding.webView.getSettings()
databasePath = activity!!.getDir("database", Context.MODE_PRIVATE).getPath()
myWebSettings!!.setJavaScriptEnabled(true)
myWebSettings!!.setDatabaseEnabled(true)
myWebSettings!!.setDatabasePath(databasePath)
myWebSettings!!.setLoadWithOverviewMode(true)
myWebSettings!!.setUseWideViewPort(true)
myWebSettings!!.setAppCacheEnabled(true)
myWebSettings!!.setCacheMode(WebSettings.LOAD_NO_CACHE)
myWebSettings!!.setDatabaseEnabled(true)
myWebSettings!!.setDomStorageEnabled(true)
myWebSettings!!.setGeolocationEnabled(false)
myWebSettings!!.setSaveFormData(false)
myWebSettings!!.setJavaScriptCanOpenWindowsAutomatically(true)
binding.webView.setWebChromeClient(object : WebChromeClient() {
private val TAG = "WebView"
override fun onConsoleMessage(cm: ConsoleMessage): Boolean {
Log.d(TAG, cm.sourceId() + ": Line " + cm.lineNumber() + " : " + cm.message())
return true
}
})
binding.completeButton.singleClick {
showCompleteCOnfirmationDialog()
}
binding.cancelButton.singleClick {
val cancelDialogFragment = CancelDialogFragment()
cancelDialogFragment.arguments = bundleOf("participant" to participant)
cancelDialogFragment.show(fragmentManager!!)
}
val intakeRequest = IntakeRequest("")
intakeRequest.meta = participant!!.meta
viewModel.setParticipant(intakeRequest, participant?.screeningId)
binding.webView.addJavascriptInterface(JavascriptInterface(activity, viewModel, jobManager), "Android")
viewModel.intakePostComplete?.observe(this, Observer { assertsResource ->
if (assertsResource?.status == Status.SUCCESS) {
println(assertsResource.data?.data)
if (assertsResource.data != null) {
val intakeData = assertsResource.data.data
binding.webView.loadUrl(intakeData?.intake_url)
webUrl = intakeData!!.intake_url
Log.d("INTAKE_FRAGMENT", "URL: " + webUrl)
}
}
})
binding.webView.setWebViewClient(object : WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
if(url != null) {
//binding.webView.loadUrl(webUrl)
}
}
override fun onReceivedHttpError(view: WebView?, request: WebResourceRequest?, errorResponse: WebResourceResponse?
) {
super.onReceivedHttpError(view, request, errorResponse)
Log.d("INTAKE_READING","HTTP_ERROR_REQUEST: " + request.toString() + " AND HTTP_ERROR_RESPONSE " + errorResponse.toString())
}
override fun onReceivedHttpAuthRequest(
view: WebView?,
handler: HttpAuthHandler?,
host: String?,
realm: String?
) {
super.onReceivedHttpAuthRequest(view, handler, host, realm)
Log.d("INTAKE_READING" , "HTTP_AUTH_REQUEST_ERROR: " + realm + ", " + host)
}
})
viewModel.intakeUpdateComplete?.observe(this, Observer { assertsResource ->
if (assertsResource?.status == Status.SUCCESS) {
println(assertsResource.data?.data)
if (assertsResource.data != null) {
val completedDialogFragment = CompletedDialogFragment()
completedDialogFragment.arguments = bundleOf("is_cancel" to false)
completedDialogFragment.show(fragmentManager!!)
} else {
binding.completeButton.visibility = View.VISIBLE
toast(assertsResource.message.toString())
Crashlytics.logException(Exception("IntakeComplete " + assertsResource.message.toString()))
binding.executePendingBindings()
}
}
})
}
class JavascriptInterface(
val mContext: FragmentActivity?,
val viewModel: IntakeReadingsViewModel,
val jobManager: JobManager
) {
fun finish() {
}
fun showAndroidToast() {
Toast.makeText(mContext, "ss", Toast.LENGTH_LONG).show();
}
@android.webkit.JavascriptInterface
fun showToast(json: String) {
}
private fun isNetworkAvailable(): Boolean {
val connectivityManager = mContext?.getSystemService(Context.CONNECTIVITY_SERVICE)
return if (connectivityManager is ConnectivityManager) {
val networkInfo: NetworkInfo? = connectivityManager.activeNetworkInfo
networkInfo?.isConnected ?: false
} else false
}
}