<kotlin>基础,杂七杂八(亲测有效)
okhttp
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val url="http://localhost:8080/kotlin/hello.json" var client=OkHttpClient() var request=Request.Builder().url(url).get().build() client.newCall(request).enqueue(object:Callback{ override fun onFailure(call: Call, e: IOException) { } override fun onResponse(call: Call, response: Response) { println("返回的内容是-----------------"+response.body?.string()) }}) // readPage() }
gson解析json
1.简单测试
MainActivity.kt
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val string="""{"name":"jack","age":"20","hobbies":{"indoor":"music","outdoor":"basketball"},"other":[{"workin":"baidu","duration":"3years"},{"workin":"google","duration":"1years"}]}""" val gson:Gson= Gson() try { val student = gson.fromJson<Student>(string, Student::class.java) println(student.hobbies?.indoor) println(student.other[1].workin) println("解析json成功") }catch (e:Exception){ println("解析json失败") } } }
Student.kt
open class Student { //{"name":"jack","age":"20","hobbies":{"indoor":"music","outdoor":"basketball"},"other":[{"workin":"baidu","duration":"3years"},{"workin":"google","duration":"1years"}]} var name:String?=null var age:Int?=null var hobbies:Hobbies?=null var other= listOf<Other>() } open class Hobbies() { var indoor:String?= null var outdoor:String?=null } open class Other(){ var workin:String?=null var duration:String?=null }
2.解析QQ音乐MV界面返回的json
写这个真心花了好长的时间。。那个发送get请求的url有空再研究下
MainActivity.kt
class MainActivity : AppCompatActivity() ,AnkoLogger{ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val url="https://u.y.qq.com/cgi-bin/musicu.fcg?-=mvlib9586712011620675&g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq.json&needNewCode=0&data=%7B%22comm%22%3A%7B%22ct%22%3A24%7D%2C%22mv_list%22%3A%7B%22module%22%3A%22MvService.MvInfoProServer%22%2C%22method%22%3A%22GetAllocMvInfo%22%2C%22param%22%3A%7B%22start%22%3A0%2C%22size%22%3A20%2C%22version_id%22%3A7%2C%22area_id%22%3A19%2C%22order%22%3A0%7D%7D%7D&mv_list" //val url="http://localhost:8080/kotlin/hotel.json" var client=OkHttpClient() var request=Request.Builder().url(url).get().build() client.newCall(request).enqueue(object:Callback{ override fun onFailure(call: Call, e: IOException) { } override fun onResponse(call: Call, response: Response) { val gson: Gson= Gson() try { val mvListResp=gson.fromJson<MvListResp>(response.body?.string(),MvListResp::class.java)//object:TypeToken<MvListResp>(){}.type println(mvListResp.mv_list?.data?.list!![0].singers[0].name) } catch (e: Exception) { println("解析JSON数据失败") e.printStackTrace() } }}) } }
MVListResponse.kt
class MvListResp { var code: Int? = null var ts: Long? = null var mv_list: MvList? = null } class MvList() { var code: Int? = null var data:MvListData?=null var total: Int? = null } class MvListData(){ var list= listOf<MvInfo>() } class MvInfo() { var comment_cnt: Int? = null var diff: Int? = null var duration: Int? = null var has_fav: Int? = null var has_star: Int? = null var mv_switch: Int? = null var mvid: Int? = null var picurl: String? = null var playcnt: Int? = null var pubdate: Long? = null var score: Int? = null var singers = listOf<SingeInfo>() var star_cnt: Int? = null var subtitle: String? = null var title: String? = null var uploader: Uploader? = null var vid: String? = null } class SingeInfo() { var id: Int? = null var mid: String? = null var name: String? = null var picurl: String? = null } class Uploader() { var enc_uin: String? = null var headurl: String? = null var nick: String? = null }
get请求返回json是这么一长串
{"code":0,"ts":1580987499140,"mv_list":{"code":0,"data":{ "list": [ { "comment_cnt": 3472, "diff": 0, "duration": 215, "has_fav": 0, "has_star": 0, "mv_switch": 31, "mvid": 1589332, "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T015R640x360M102002NciZ91bkihl.jpg", "playcnt": 3299374, "pubdate": 1577030400, "score": 0, "singers": [ { "id": 283148, "mid": "004NfJdF0gn6Z1", "name": "Red Velvet (레드벨벳)", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M000004NfJdF0gn6Z1.jpg" } ], "star_cnt": 10082, "subtitle": "", "title": "Red Velvet《Psycho》MV", "uploader": { "enc_uin": "", "headurl": "", "nick": "" }, "vid": "v0033f0aqxz" }, { "comment_cnt": 10069, "diff": 0, "duration": 193, "has_fav": 0, "has_star": 0, "mv_switch": 31, "mvid": 1517510, "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T015R640x360M1010003x77W1X0FA3.jpg", "playcnt": 15624622, "pubdate": 1554307200, "score": 0, "singers": [ { "id": 1190986, "mid": "003DBAjk2MMfhR", "name": "BLACKPINK", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M000003DBAjk2MMfhR.jpg" } ], "star_cnt": 52452, "subtitle": "", "title": "Kill This Love", "uploader": { "enc_uin": "", "headurl": "", "nick": "" }, "vid": "l0030x5i9ek" }, { "comment_cnt": 2111, "diff": 0, "duration": 209, "has_fav": 0, "has_star": 0, "mv_switch": 31, "mvid": 1595732, "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T015R640x360M101000eUr1s1uVAnh.jpg", "playcnt": 269312, "pubdate": 1580140800, "score": 0, "singers": [ { "id": 5675, "mid": "001NJAY04JwuK4", "name": "SUPER JUNIOR (슈퍼주니어)", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M000001NJAY04JwuK4.jpg" } ], "star_cnt": 3686, "subtitle": "", "title": "2YA2YAO!", "uploader": { "enc_uin": "", "headurl": "", "nick": "" }, "vid": "x00339hsdnm" }, { "comment_cnt": 2650, "diff": 0, "duration": 210, "has_fav": 0, "has_star": 0, "mv_switch": 31, "mvid": 1580456, "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T015R640x360M1010005WtQx0DKH4x.jpg", "playcnt": 2769098, "pubdate": 1573660800, "score": 0, "singers": [ { "id": 176744, "mid": "001p5MSX07YGwi", "name": "MAMAMOO (마마무)", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M000001p5MSX07YGwi.jpg" } ], "star_cnt": 9816, "subtitle": "", "title": "HIP", "uploader": { "enc_uin": "", "headurl": "", "nick": "" }, "vid": "u0032piim9h" }, { "comment_cnt": 5164, "diff": 0, "duration": 215, "has_fav": 0, "has_star": 0, "mv_switch": 31, "mvid": 1474621, "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T015R640x360M101002LaPiL3SyAOs.jpg", "playcnt": 11125344, "pubdate": 1528992000, "score": 0, "singers": [ { "id": 1190986, "mid": "003DBAjk2MMfhR", "name": "BLACKPINK", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M000003DBAjk2MMfhR.jpg" } ], "star_cnt": 15351, "subtitle": "", "title": "DDU-DU DDU-DU (뚜두뚜두)", "uploader": { "enc_uin": "", "headurl": "", "nick": "" }, "vid": "v00260mpcch" }, { "comment_cnt": 2868, "diff": 0, "duration": 252, "has_fav": 0, "has_star": 0, "mv_switch": 31, "mvid": 137284, "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T015R640x360M1030015eqbW1zwkBk.jpg", "playcnt": 112053964, "pubdate": 1342454400, "score": 0, "singers": [ { "id": 13143, "mid": "002vbc3A2bv88T", "name": "PSY (朴载相)", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M000002vbc3A2bv88T.jpg" }, { "id": 10415, "mid": "0040tgjy2qm5bG", "name": "泫雅 (현아)", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M0000040tgjy2qm5bG.jpg" } ], "star_cnt": 17357, "subtitle": "", "title": "강남스타일 (Gangnam Style)", "uploader": { "enc_uin": "", "headurl": "", "nick": "" }, "vid": "j0015nxvoyg" }, { "comment_cnt": 5225, "diff": 0, "duration": 197, "has_fav": 0, "has_star": 0, "mv_switch": 31, "mvid": 1578560, "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T015R640x360M102003HhoWB1cqsNo.jpg", "playcnt": 3483598, "pubdate": 1572883200, "score": 0, "singers": [ { "id": 10415, "mid": "0040tgjy2qm5bG", "name": "泫雅 (현아)", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M0000040tgjy2qm5bG.jpg" } ], "star_cnt": 16658, "subtitle": "", "title": "FLOWER SHOWER", "uploader": { "enc_uin": "", "headurl": "", "nick": "" }, "vid": "l003273bvdk" }, { "comment_cnt": 2953, "diff": 0, "duration": 176, "has_fav": 0, "has_star": 0, "mv_switch": 31, "mvid": 1495127, "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T015R640x360M101003wxGgR0cUxSv.jpg", "playcnt": 10662355, "pubdate": 1541952000, "score": 0, "singers": [ { "id": 156564, "mid": "004QK4Dt0N2OX9", "name": "Jennie (제니)", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M000004QK4Dt0N2OX9.jpg" } ], "star_cnt": 25444, "subtitle": "", "title": "SOLO", "uploader": { "enc_uin": "", "headurl": "", "nick": "" }, "vid": "m00284h398b" }, { "comment_cnt": 1249, "diff": 0, "duration": 194, "has_fav": 0, "has_star": 0, "mv_switch": 31, "mvid": 1592395, "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T015R640x360M101002IdYdQ2ZZzOx.jpg", "playcnt": 879905, "pubdate": 1578412800, "score": 0, "singers": [ { "id": 940913, "mid": "0029SxgR215af5", "name": "伯贤 (백현)", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M0000029SxgR215af5.jpg" } ], "star_cnt": 4356, "subtitle": "", "title": "너를 사랑하고 있어 (《浪漫医生金师傅2》韩国电视剧插曲)", "uploader": { "enc_uin": "", "headurl": "", "nick": "" }, "vid": "z0033w2kkzc" }, { "comment_cnt": 3629, "diff": 0, "duration": 216, "has_fav": 0, "has_star": 0, "mv_switch": 31, "mvid": 1323921, "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T015R640x360M101000fzlOv4HuKAE.jpg", "playcnt": 9586479, "pubdate": 1498060800, "score": 0, "singers": [ { "id": 1190986, "mid": "003DBAjk2MMfhR", "name": "BLACKPINK", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M000003DBAjk2MMfhR.jpg" } ], "star_cnt": 7328, "subtitle": "", "title": "As If It's Your Last (마지막처럼)", "uploader": { "enc_uin": "", "headurl": "", "nick": "" }, "vid": "m00240pahpk" }, { "comment_cnt": 843, "diff": 0, "duration": 239, "has_fav": 0, "has_star": 0, "mv_switch": 31, "mvid": 1474945, "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T015R640x360M101004ECMcc1hXlBu.jpg", "playcnt": 3314792, "pubdate": 1529424000, "score": 0, "singers": [ { "id": 1190986, "mid": "003DBAjk2MMfhR", "name": "BLACKPINK", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M000003DBAjk2MMfhR.jpg" } ], "star_cnt": 6419, "subtitle": "", "title": "Forever Young (练习室版)", "uploader": { "enc_uin": "", "headurl": "", "nick": "" }, "vid": "p0026n6t2m9" }, { "comment_cnt": 9183, "diff": 0, "duration": 191, "has_fav": 0, "has_star": 0, "mv_switch": 31, "mvid": 1062977, "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T015R640x360M1010016ZLE70SvwcL.jpg", "playcnt": 17253074, "pubdate": 1480694400, "score": 0, "singers": [ { "id": 166667, "mid": "002iE9xv2DuIQT", "name": "灿烈 (찬열)", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M000002iE9xv2DuIQT.jpg" }, { "id": 176687, "mid": "000lHQbb1VAljd", "name": "PUNCH (펀치)", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M000000lHQbb1VAljd.jpg" } ], "star_cnt": 18130, "subtitle": "", "title": "Stay With Me (《孤单又灿烂的神-鬼怪》韩剧插曲)", "uploader": { "enc_uin": "", "headurl": "", "nick": "" }, "vid": "g0022jx5u6w" }, { "comment_cnt": 1386, "diff": 0, "duration": 259, "has_fav": 0, "has_star": 0, "mv_switch": 31, "mvid": 79256, "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T015R640x360M101000c88Ej3pIF7Z.jpg", "playcnt": 29472192, "pubdate": 1322668800, "score": 0, "singers": [ { "id": 37726, "mid": "004DOoCo0hMi1y", "name": "Trouble Maker (트러블메이커)", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M000004DOoCo0hMi1y.jpg" } ], "star_cnt": 5660, "subtitle": "", "title": "Trouble Maker (麻烦制造者)", "uploader": { "enc_uin": "", "headurl": "", "nick": "" }, "vid": "f0016e85b12" }, { "comment_cnt": 3704, "diff": 0, "duration": 230, "has_fav": 0, "has_star": 0, "mv_switch": 31, "mvid": 917352, "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T015R640x360M101002nqSlZ0xFMQz.jpg", "playcnt": 9602424, "pubdate": 1470585600, "score": 0, "singers": [ { "id": 1190986, "mid": "003DBAjk2MMfhR", "name": "BLACKPINK", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M000003DBAjk2MMfhR.jpg" } ], "star_cnt": 4942, "subtitle": "", "title": "휘파람 (WHISTLE) (口哨)", "uploader": { "enc_uin": "", "headurl": "", "nick": "" }, "vid": "h00214mr981" }, { "comment_cnt": 1982, "diff": 0, "duration": 195, "has_fav": 0, "has_star": 0, "mv_switch": 31, "mvid": 1343826, "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T015R640x360M102002XpS6f0FBWxF.jpg", "playcnt": 3401572, "pubdate": 1500307200, "score": 0, "singers": [ { "id": 38578, "mid": "001QVwtq3l8cKC", "name": "EXO (엑소)", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M000001QVwtq3l8cKC.jpg" } ], "star_cnt": 7020, "subtitle": "", "title": "Ko Ko Bop (韩文版) (叩叩趴)", "uploader": { "enc_uin": "", "headurl": "", "nick": "" }, "vid": "i0029u40k89" }, { "comment_cnt": 639, "diff": 0, "duration": 279, "has_fav": 0, "has_star": 0, "mv_switch": 31, "mvid": 1589954, "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T015R640x360M101003Ax7uT1Vmi6f.jpg", "playcnt": 451546, "pubdate": 1577116800, "score": 0, "singers": [ { "id": 14207, "mid": "004Bjyj52RTYOj", "name": "IU (아이유)", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M000004Bjyj52RTYOj.jpg" } ], "star_cnt": 3613, "subtitle": "", "title": "Blueming (Live At 2019 IU Tour Concert 'Love, poem')", "uploader": { "enc_uin": "", "headurl": "", "nick": "" }, "vid": "s0033lji4ma" }, { "comment_cnt": 1939, "diff": 0, "duration": 186, "has_fav": 0, "has_star": 0, "mv_switch": 31, "mvid": 1595834, "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T015R640x360M101000EtreD4afOBm.jpg", "playcnt": 59366, "pubdate": 1580313600, "score": 0, "singers": [ { "id": 38578, "mid": "001QVwtq3l8cKC", "name": "EXO (엑소)", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M000001QVwtq3l8cKC.jpg" }, { "id": 198895, "mid": "000xVBxt1xgiyW", "name": "QQ音乐", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M000000xVBxt1xgiyW.jpg" } ], "star_cnt": 2733, "subtitle": "", "title": "第29届首尔歌谣大赏QQ音乐最受欢迎K-POP Artist奖: EXO (2020 首尔歌谣大赏)", "uploader": { "enc_uin": "", "headurl": "", "nick": "" }, "vid": "r0033sasmup" }, { "comment_cnt": 446, "diff": 0, "duration": 330, "has_fav": 0, "has_star": 0, "mv_switch": 31, "mvid": 1594591, "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T015R640x360M101002kMApV4QyioQ.jpg", "playcnt": 264598, "pubdate": 1579190400, "score": 0, "singers": [ { "id": 162691, "mid": "001TpDgn4SxyJn", "name": "BTS (防弹少年团)", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M000001TpDgn4SxyJn.jpg" } ], "star_cnt": 2029, "subtitle": "", "title": "Black Swan (Art Film performed by MN Dance Company)", "uploader": { "enc_uin": "", "headurl": "", "nick": "" }, "vid": "u00335is4ma" }, { "comment_cnt": 1888, "diff": 0, "duration": 206, "has_fav": 0, "has_star": 0, "mv_switch": 31, "mvid": 1499989, "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T015R640x360M101003SWIpY2dFdux.jpg", "playcnt": 3186092, "pubdate": 1544716800, "score": 0, "singers": [ { "id": 38578, "mid": "001QVwtq3l8cKC", "name": "EXO (엑소)", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M000001QVwtq3l8cKC.jpg" } ], "star_cnt": 11878, "subtitle": "", "title": "Love Shot (Live At Music Bank 2018\/12\/14)", "uploader": { "enc_uin": "", "headurl": "", "nick": "" }, "vid": "c0029hx0acq" }, { "comment_cnt": 326, "diff": 0, "duration": 215, "has_fav": 0, "has_star": 0, "mv_switch": 31, "mvid": 1592451, "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T015R640x360M102001iiaqZ0EDYn8.jpg", "playcnt": 225894, "pubdate": 1578412800, "score": 0, "singers": [ { "id": 283148, "mid": "004NfJdF0gn6Z1", "name": "Red Velvet (레드벨벳)", "picurl": "http:\/\/y.gtimg.cn\/music\/photo_new\/T001R150x150M000004NfJdF0gn6Z1.jpg" } ], "star_cnt": 1255, "subtitle": "", "title": "Red Velvet《Psycho》Performance Video (舞蹈版)", "uploader": { "enc_uin": "", "headurl": "", "nick": "" }, "vid": "s0033u4mq9z" } ], "total": 1000 }}}
可以试着把读取到的数据加载到视图。。
简单的适配
复杂点的适配
关键代码
private var list= ArrayList<MvInfo>() fun updateList(list:List<MvInfo>){ this.list.clear() this.list.addAll(list) notifyDataSetChanged() }
源码
链接:https://pan.baidu.com/s/1NAtDi6t0fdKQYs26ewNycQ
提取码:rmgo