一、项目地址:Github
-
如果你的项目 Gradle 配置是在
7.0 以下
,需要在build.gradle
文件中加入(默认jdk1.8)allprojects { repositories { //远程仓库:https://jitpack.io maven { url 'https://jitpack.io' } } }
-
如果你的 Gradle 配置是
7.0 及以上
,则需要在settings.gradle
文件中加入(默认jdk11)dependencyResolutionManagement { repositories { //远程仓库:https://jitpack.io maven { url 'https://jitpack.io' } } }
-
在项目 app 模块下的 build.gradle 文件中加入远程依赖和kapt插件
plugins {
id 'kotlin-kapt'
}
dependencies {
//ktor网络框架
implementation("io.ktor:ktor-client-core:1.6.0")
//ktor扩展库
implementation("com.github.hearthappy.viewmodelautomation:ktor-expand:2.0.4")
//注解库
compileOnly("com.github.hearthappy.viewmodelautomation:annotations:2.0.4")
//处理注解自动生成库
kapt("com.github.hearthappy.viewmodelautomation:processor:2.0.4")
}
@AndroidViewModel
@BindLiveData("getImages", ReImages::class, ResImages::class)
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
/**
* GET:获取图片
* @property page Int
* @property size Int
* @constructor
*/
@Request(urlString = "/getImages")
data class ReImages(val page: Int, val size: Int)
//Activity中添加
private val viewModel by viewModels<MainViewModel>()
//监听
lifecycleScope.launchWhenCreated {
viewModel.getImagesStateFlow.collect {
when (it) {
is RequestState.LOADING-> progress.show()
is RequestState.SUCCEED -> tvResult.showSucceedMsg(it.body.toString())
is RequestState.FAILED -> tvResult.showFailedMsg(it)
is RequestState.Throwable -> tvResult.showThrowableMsg(it)
else -> Unit
}
}
}
(2)ViewModel生成路径:build/generated/source/kaptKotlin/debug/com/hearthappy/compiler/viewmodel/MainViewModel.kt
@Request(urlString = "/getImages")
data class ReImages(val page: Int, val size: Int)
/**
*方式一:ReRegister为Body
*
*/
@Request(Http.POST, urlString = "/register")
@Body
data class ReRegister(val account: String, val code: String, val password: String)
/**
* 方式二:需要传入动态header或REST时
*/
@Request(Http.POST, urlString = "/login")
data class ReLogin(@Header("X-Auth-Token") val token:String,@Body val loginBody: LoginBody)
data class LoginBody(val account: String, val password: String)
@Request(Http.POST, urlString = "/login")
data class ReLogin(@Body(BodyType.FormUrlEncoded) val loginBody: LoginBody)
data class LoginBody(@Query("account")val account: String,@Query("password") val password: String)
@Request(Http.PATCH, urlString = "/identity/v3/users/{userid}")
data class ReUpdatePwd(@Header("X-Auth-Token") val token: String, val userid: String, @Body(BodyType.JSON) val updatePasswordBody: UpdatePasswordBody)
data class UpdatePasswordBody(val user:UserBean){
data class UserBean(val password:String)
}
@Request(Http.DELETE,"/veaudit/v2/vir_msg/{instance_id}")
data class ReErrorMessReset(@Header("X-Auth-Token") val token: String,val instance_id:String)
/**
*
* @property token String
* @property flavorid String :REST(注意:参数flavorid名称必须与{flavorid}一致 )
* @constructor
*/
@Request(urlString = "/compute/v2.1/flavors/{flavorid}")
data class ReVMHardwareInfo(@Header("X-Auth-Token") val token: String, val flavorid: String)
/**
* 一、静态全局配置(支持动态baseURL、动态代理、网络日志输出)
*/
@Service
@ServiceConfig(baseURL = "https://api.apiopen.top/api")
class MyApplication: Application() {
override fun onCreate() {
super.onCreate()
}
}
/**
* 二:动态配置
*(注:defaultConfig()函数。根据@ServiceConfig注解参数“key”生成)
*
*/
class LoginActivity: BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//动态修改全局配置
application.defaultConfig().apply {
baseURL = "http://xxx.xxx.com"
proxyIP = "xxx.xxx.x.x"
proxyPort = 8888
enableLog = true
}
}
}
/**
* @annotation @Headers([ContentType.xxx.xxx]):固定Headers
* @property token String :动态Header
* @property instance_id String REST
* @constructor
*/
@Headers([ContentType.Application.Json])
@Request(Http.DELETE,"/veaudit/v2/vir_msg/{instance_id}")
data class ReErrorMessReset(@Header("X-Auth-Token") val token: String,val instance_id:String)
@AndroidViewModel
@BindStateFlow("test")
class LoginActivity : AppCompatActivity()
生成代码如下:
public fun test(io: suspend () -> HttpResponse): Unit {
_testStateFlow.value = RequestState.LOADING
requestScope<String>(
io = io,
onFailure = { _testStateFlow.value = RequestState.FAILED(it) },
onSucceed = { body, response ->
_testStateFlow.value = RequestState.SUCCEED(body,response)
},
onThrowable = { _testStateFlow.value = RequestState.Throwable(it) }
)
}
suspend inline fun <reified T> login() = ktorClient().use {
it.get<T>("https://ktor.io/") {
header(HttpHeaders.ContentType, ContentType.Application.Json)
parameter("price","asc")
}
}
viewModel.test{ login() }
参数 | 描述 | 必须 | 默认值 |
---|---|---|---|
viewModelClassName | 自定义ViewModel类名 | 否 | 根据Activity和Fragment前缀生成。例如:LoginActivity则生成LoginViewModel |
参数 | 描述 | 必须 | 默认值 |
---|---|---|---|
methodName | 自定义方法名 | 是 | 无 |
requestClass | 标记@Request注解的请求类 | 否 | 默认生成:io参数,参数是一个挂起函数 |
responseClass | Json数据转换的实体类 | 否 | 默认返回String类型的文本 |
liveDataName\stateFlowName | 自定义LiveData\StateFlow属性名 | 否 | 默认生成:methodName+LiveData\StateFlow |
参数 | 描述 | 必须 | 默认值 |
---|---|---|---|
type | 指定 HTTP 方法(支持:GET、POST、PATCH、DELETE) | 否 | GET |
urlString | URL字符串(域、路径、查询参数等) | 是 | 无 |
serviceKey | 使用多个全局配置时,可通过key关联指定全局配置 | 否 | defaultConfig |
参数 | 描述 | 必须 | 默认值 |
---|---|---|---|
baseURL | 域 | 是 | 无 |
enableLog | 输出网络日志 | 否 | true |
proxyIp | 代理IP | 否 | 无 |
proxyPort | 代理端口 | 否 | 无 |
key | 关联指定请求key | 否 | defaultConfig |