当我尝试使用以下方法加载网址时:
webView.loadUrl("https://example.com"),
和
webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
view?.loadUrl(url)
return true
}
}
结果为空白。
我认为是因为URL被重定向到另一个链接,但是如何解决呢?
答案 0 :(得分:0)
您有<uses-permission android:name="android.permission.INTERNET" />
吗?
答案 1 :(得分:0)
这是样品溶液。 试试吧。
1.activity_main.xml
var data = mockDataDB.Data.AsQueryable()
.Select(x => new ProductDto
{
Id = Int64.TryParse(x.id, out long val) ? val : 0L,
Quantity = Int32.TryParse(x.quantity, out int somenumber) ? somenumber : (int?)null
}
2)MainActivity.kt
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.kotlinwebview.MainActivity">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
答案 2 :(得分:0)
这里是完整的答案。 在MainActivity.kt中相应地更改URL。 MainAcivity.kt
class MainActivity : AppCompatActivity() {
@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val mWebView = findViewById<WebView>(R.id.webview)
val webSettings = mWebView.settings
webSettings.javaScriptEnabled = true
mWebView.loadUrl(getString(R.string.website_url))
mWebView.webViewClient = HelloWebViewClient()
WebView.setWebContentsDebuggingEnabled(false)
}
private inner class HelloWebViewClient : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
if (Uri.parse(url).host == getString(R.string.website_domain)) {
return false
}
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
return true
}
override fun onPageFinished(view: WebView, url: String) {
// TODO Auto-generated method stub
super.onPageFinished(view, url)
}
}
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) {
webview.goBack()
return true
}
return super.onKeyDown(keyCode, event)
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.learnkotlin">
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter
android:autoVerify="true"
tools:targetApi="m">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="quaestio.org"
android:scheme="https" />
</intent-filter>
</activity>
<meta-data
android:name="android.webkit.WebView.EnableSafeBrowsing"
android:value="true" />
</application>
</manifest>
String.xml
<resources>
<string name="app_name">LearnKotlin</string>
<string name="website_domain">quaestio.org</string>
<string name="website_url">https://quaestio.org</string>
</resources>
我已经测试过,它绝对可以正常工作。如果您遇到问题,请告诉我。 祝你好运。