<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() }View Code
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失败") } } }View Code
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 }View Code
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() } }}) } }View Code
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 }View Code
标签:Int,String,杂七杂八,22%,有效,var,null,class,亲测 来源: https://www.cnblogs.com/vocus/p/12267544.html