fun getSer() { // 创建一个URL对象 val url = URL("http://api.mairui.club/hsrl/ssjy/000001/") // 创建一个HttpURLConnection对象 val connection = url.openConnection() as HttpURLConnection // 设置请求方式 connection.requestMethod = "GET" // 设置连接超时时间 connection.connectTimeout = 10000 // 设置读取超时时间 connection.readTimeout = 10000 // 获取输入流 val inputStream = connection.inputStream // 将输入流转换成字符串 val response = inputStream.bufferedReader().use { it.readText() } // 关闭输入流 inputStream.close() // 正则表达式 val pattern = Pattern.compile(R.string.regex.toString()) // 创建Matcher对象 val matcher = pattern.matcher(response) // 如果找到匹配的字符串 if (matcher.find()) { // 获取匹配的字符串 val fm = matcher.group(1) val h = matcher.group(2) val hs = matcher.group(3) val lb = matcher.group(4) val l = matcher.group(5) val lt = matcher.group(6) val o = matcher.group(7) val pe = matcher.group(8) val pc = matcher.group(9) val p = matcher.group(10) val sz = matcher.group(11) val cje = matcher.group(12) val ud = matcher.group(13) val v = matcher.group(14) val yc = matcher.group(15) val zf = matcher.group(16) val zs = matcher.group(17) val sjl = matcher.group(18) val zdf60 = matcher.group(19) val zdfnc = matcher.group(20) val t = matcher.group(21) // 将获取的字符串写入到TextView中 /textView1.text = fm textView2.text = h textView3.text = hs textView4.text = lb textView5.text = l textView6.text = lt textView7.text = o textView8.text = pe textView9.text = pc textView10.text = p textView11.text = sz textView12.text = cje textView13.text = ud textView14.text = v textView15.text = yc textView16.text = zf textView17.text = zs textView18.text = sjl textView19.text = zdf60 textView20.text = zdfnc textView21.text = t }
以get方式访问网页,通过正则表达式匹配数据后写入指定textview
最新推荐文章于 2024-09-14 14:49:49 发布