Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.68 KB | None | 0 0
  1. package com.example.roomfinance.model
  2.  
  3. class NetHelper : AsyncTask<URL, Int, ArrayList<Valute>>() {
  4.    
  5.  
  6.     override fun doInBackground(vararg urls: URL): ArrayList<Valute> {
  7.  
  8.         val empList: ArrayList<Valute> = ArrayList()
  9.         val url = URL("https://www.cbr-xml-daily.ru/daily.xml")
  10.  
  11.         val builderFactory = DocumentBuilderFactory.newInstance()
  12.         val docBuilder = builderFactory.newDocumentBuilder()
  13.         val doc = docBuilder.parse(url.openStream())
  14.  
  15.         val nList = doc.getElementsByTagName("Valute")
  16.         for (i in 0 until nList.length) {
  17.  
  18.             val element = nList.item(i) as Element
  19.             empList.add(
  20.                 Valute(
  21.                     getNodeValue("NumCode", element),
  22.                     getNodeValue("CharCode", element),
  23.                     getNodeValue("Nominal", element),
  24.                     getNodeValue("Name", element),
  25.                     getNodeValue("Value", element)
  26.                 )
  27.             )
  28.         }
  29.  
  30.         return empList
  31.     }
  32.  
  33.     override fun onProgressUpdate(vararg values: Int?) {
  34.         super.onProgressUpdate(*values)
  35.     }
  36.  
  37.     override fun onPostExecute(result: ArrayList<Valute>) {
  38.         super.onPostExecute(result)
  39.  
  40.     }
  41.  
  42.     fun getNodeValue(tag: String, element: Element): String{
  43.         val nodeList = element.getElementsByTagName(tag)
  44.         val node = nodeList.item(0)
  45.         if(node != null){
  46.             if(node.hasChildNodes()){
  47.                 val child = node.firstChild
  48.                 while(child!=null){
  49.                     if(child.nodeType == Node.TEXT_NODE) return child.nodeValue
  50.                 }
  51.             }
  52.         }
  53.         return ""
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement