当前位置: 首页 > news >正文

kotlin: 用Retrofit+协程访问网络接口

一,安装第三方库

build.gradle.kts中添加

    implementation("com.squareup.retrofit2:retrofit:2.9.0")implementation("com.squareup.retrofit2:converter-gson:2.9.0")implementation("com.google.code.gson:gson:2.13.0")

然后点击:Sync Now 链接

二,代码:

json.php

image

接口model类

package com.example.okdemo2.netimport androidx.annotation.Keep
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.Serializable//@InternalSerializationApi
/*** 请求返回的BasicResponse 可以自己重新实现*/@Keep
@Serializable
open class ApiResponse<T>(open var data: T? = null,open var code: Int = -1,open var msg: String = "",open var status: String? = null,open var time: String? = null,
) {val isSuccess: Booleanget() = code == 200}

数据model类

package com.example.okdemo2.fragment//import com.google.gson.annotations.
//import com.google.gson.annotations.SerializedName
//import java.io.Serializable
import androidx.annotation.Keep
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable@Keep
@Serializable
data class UserList(@SerialName("list")val `list`: MutableList<UserListItem>,@SerialName("curPage")val curPage: Int,@SerialName("total")val total: Int,@SerialName("totalPage")val totalPage: Int,@SerialName("token")val token: String,
)@Keep
@Serializable
data class UserListItem(@SerialName("id")val id: Int,@SerialName("name")val name: String,@SerialName("image")val image: String,
)

接口列表:

package com.example.okdemo2.fragmentimport com.example.okdemo2.net.ApiResponse
import retrofit2.http.GET
import retrofit2.http.QueryMapinterface ApiService {@GET("/json.php")suspend fun userList(@QueryMap map: MutableMap<String, Any>): ApiResponse<UserList>
}

调用retrofit访问接口

        //按钮点击binding.getJsonButton.setOnClickListener {println("按钮被点击了")val retrofit = Retrofit.Builder().baseUrl("http://www.nihonnoma.net").addConverterFactory(GsonConverterFactory.create()).build()val appService = retrofit.create(ApiService::class.java)lifecycleScope.launch {//参数val params = mutableMapOf<String, Any>("p" to 1,"count" to 20,"token" to "王富贵到此一游")//访问接口val user = appService.userList(params)println(user)if (user.status == "success") {println("成功")println(user.data)println("当前页:"+user.data?.curPage)println("当前用户:"+user.data?.token)println(user.data?.list)} else {println("失败")println("错误代码:"+user.code)println("错误信息:"+user.msg)}}}

三,运行结果 :

image

http://www.sczhlp.com/news/75840/

相关文章:

  • kotlin: 报错:@OptIn(kotlinx.serialization.InternalSerializationApi::class)
  • android studio: 切换项目的gradle版本
  • 公司网站可以自己做深圳万户网络技术有限公司
  • 一般建设网站需要多少预算有哪些做ppt用图片的网站
  • 网站上设置多语言怎么做在线制作网站的平台
  • sem包括网站建设吗深圳做网站优化工资多少
  • Typora小教程
  • 海门公司网站制作费用深圳商业网站建设模板
  • 婚纱照网站模板做健身网站开题报告
  • asp.net mvc5网站开发之美 pdf郑州软件开发公司招聘
  • 网站内容有哪些衡水网站建设03181688
  • 848给我做一下88网站专业网站建设公司在线咨询
  • 做电影网站的服务器百度建站
  • 内部网站链接怎么做做一个软件需要哪些步骤
  • 网站设计 深圳个人网站购买
  • 网站源码偷取工具网站开发的工作经验要求
  • 食品网站建设书网店装修流程
  • 重庆在线网站推广北京旅游设计网站建设
  • 简述网站开发平台网络加速器免费永久版
  • 什么网站可以做音乐伴奏城市门户网站模板
  • 网站建设开发程序网站建设 深圳信科公司
  • 什么是网站开发中的分页网站制作结算确认函
  • 宁波网站建设caiyiduoseo优化实训报告
  • Nest.js 怎样使用 hbs 的 partials
  • shell案例
  • 秀山网站建设公司泰安做网站
  • 股票网站模板网站vr视角怎么做
  • 自己可以做装修效果图的网站怎么找厂家生产产品
  • 中牟高端网站建设生鲜网站建设
  • 深圳做网站哪家公司好国内做免费的视频网站有哪些