以get方式访问网页,通过正则表达式匹配数据后写入指定textview

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
   }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值