开发者社区> 问答> 正文

通过WebView的两个URL

我已使用以下代码在Kotlin WebView中成功呈现了我的Web应用程序。但是,我无法登录。我正在使用单独的身份服务器-它使用不同的URL(请参阅代码中的secondurl值)。

如何启用从Web视图中调用此URL进行登录身份验证?


package com.example.mywebview

import android.os.Bundle
import android.util.Log
import android.webkit.ConsoleMessage
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

        private val firsturl = "https://main.mysite.net/"
        private val secondurl = "https://id.mysite.net/"

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)

            // Get the web view settings instance
            val setting = webview.settings

            // Enable java script in web view
            setting.javaScriptEnabled = true

            // Maintain webview in app
            webview.webViewClient = WebViewClient()

            webview.loadUrl(firsturl)

        }
}

展开
收起
Puppet 2020-01-15 10:42:21 791 0
2 条回答
写回答
取消 提交回答
  • 楼上为啥自问自答啊,我不是很明白,这样做的意义是什么呢?

    2020-03-26 21:26:29
    赞同 展开评论 打赏
  • 如果要在webview中打开网页登录名,则可以使用网页上提供的按钮,并且应该启用javascript(因为它可能包含用于登录的功能)。

    如果那不是您希望的另一种选择:

    发送请求到服务器 从服务器端的php获取字符串或JSON 根据响应做你需要做的

    
     val textView = findViewById<TextView>(R.id.text)
    // ...
    
    // Instantiate the RequestQueue.
    val queue = Volley.newRequestQueue(this)
    val url = "http://www.google.com"
    
    // Request a string response from the provided URL.
    val stringRequest = StringRequest(Request.Method.GET, url,
        Response.Listener<String> { response ->
            // Display the first 500 characters of the response string.
            textView.text = "Response is: ${response.substring(0, 500)}"
        },
        Response.ErrorListener { textView.text = "That didn't work!" })
    
    // Add the request to the RequestQueue.
    queue.add(stringRequest)
    

    来源:https : //developer.android.com/training/volley/simple#kotlin

    2020-01-15 10:42:49
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
相关产品:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载