0.說明 :

利用 Gson library 將網路上 json 格式轉成 gson

1.準備 :

Gradle :

  • gson :  implementation ‘com.google.code.gson:gson:2.8.5’
  • anko :  implementation “org.jetbrains.anko:anko:0.10.8”

Data :

  • web : https://data.gov.tw/dataset/25940
  • json : https://data.tycg.gov.tw/opendata/datalist/datasetMeta/download?id=f4cc0b12-86ac-40f9-8745-885bddc18f79&rid=0daad6e6-0632-44f5-bd25-5e1de1e9146f

AndroidManifest.xml :

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
    .....
    android:usesCleartextTraffic="true"
  />

2.Code :

Layout UI :

Create empty Activity and put TextView and RecyclerView . Set id .

 

Create recyclerView item row

 

Code :

implement AnkoLogger , use Anko doAsync read URL Text

doAsync {
            val jsonPath = "http://data.tycg.gov.tw/opendata/datalist/datasetMeta/download?id=f4cc0b12-86ac-40f9-8745-885bddc18f79&rid=0daad6e6-0632-44f5-bd25-5e1de1e9146f"
            val readText = URL(jsonPath).readText()
            info(readText)
}

if have read data and next is generate class by ‘Covert Json Into Kotlin class ‘ plugin

 

 

 

 

 

 

 

 

Copy first one json data

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Use gson library get ArrayList

val gsonData = Gson().fromJson<Parking>
(readText,object:TypeToken<Parking>(){}).type)

gsonData.parkinglots.forEach{
  info(" message : ${it.address} , ${it.parkName} , ${it.introduction}")
} 

if have data and use Anko UIThread Block . SetData to RecyclerView

uiThread 
{
                result.text = "搜尋筆數有 : ${gsonData.parkingLots.size} 筆"
                parkingRecycle.run {
                    this.setHasFixedSize(true)
                    this.layoutManager = LinearLayoutManager(this@OpenDataActivity)
                    this.adapter = ParkingAdapter()
                }
}

Set Adapter and ViewHolder . inner class , block in onCreate Function

inner class ParkingAdapter : RecyclerView.Adapter<ParkingViewHolder>(){
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ParkingViewHolder {
            return ParkingViewHolder(layoutInflater.inflate(R.layout.parkinglot_row,parent,false))
        }

        override fun getItemCount() = nowDat.parkingLots.size

        override fun onBindViewHolder(holder: ParkingViewHolder, position: Int) {
            holder.name.text = "Position : ${position+1}  "+nowDat.parkingLots[position].parkName
            holder.address.text = nowDat.parkingLots[position].address
            holder.itemView.tag = position
        }


    }

    inner class ParkingViewHolder(view: View) : RecyclerView.ViewHolder(view){
           val name = view.lotsName!!
           val address: TextView = view.lotsAddress!!
    }

3.Result 結果

4.Git

LoadOpenData_Kotlin_Gson,Anko

 

 

 

 

 

 

 

 

最後修改日期: 2019-05-02 19:07:21

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料