Android Studio 导入项目错误 has been compiled by a more recent version of the Java Runtime
错误日志: has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0 原因:JDK版本不对,对照关系如下 50 --> JDK1.651 --> JDK1.752 --> JDK1.853 --> JDK 954 --> JDK 1055 --> JDK 1156 --> JDK 1257 --> JDK 1358 --> JDK 1459 --> JDK 15
2023-01-18-Kotlin协程 - 协程的启动
协程构建器launch与async构建器都用来启动新协程 launch,返回一个Job并且不附带任何结果值 async,返回一个Deferred,Deferred也是一个Job,可以使用.await()在一个延期的值上得到它的最终结果。 join与await等待协程的作业启动协程@Test fun test_coroutine_builder() = runBlocking { val job = launch { delay(1000L) println("job1 finished") } val async = async { delay(1000L) println("job2 finished") "job2 result" } println(async.await()) ...
Kotlin基础 - by
除了在类属性声明的时候时候,使用 by 来获取实例 也可以在实现接口上使用 interface AService {}class MainService: AService {} class AppClient: AService by MainService() { } 从上面可以看出,AppClient 实现了 AService接口,并且指定了MainService来实例化
2023-01-17-Kotlin协程 - 结构化并发
CoroutineScope定义协程必须指定其 CoroutineScope,它会跟踪所有协程,同样它还可以取消由它所启动的所有协程。 常用的相关API有: GlobalScope,生命周期是process级别的,及时Activity或Fragment已经被销毁,协程仍然在执行。 MainScope,在Activity中使用,可以在onDestroy()中取消协程。 viewModelScope,只能在ViewModel中使用,绑定ViewModel的生命周期。 lifecycleScope,只能在Activity、Fragment中使用,会绑定Activity和Fragment的生命周期
Kotlin基础-@Jvm注解
Kotlin基础 - @JvmName、@JvmField、@JvmOverloads、@JvmStatic等
Kotlin基础 -- 变换函数、过滤函数、合并函数
变换函数map map变换函数会遍历接收者集合,让变换器函数作用于集合里的各个函数,返回结果是包含已修改的集合,会作为链上下一个函数的输入 val fruits: List<String> = listOf("Apple", "Banana", "Orange", "Lemon")val ownFruits: List<String> = fruits.map { fruit -> "I have $fruit" } .map { owner -> "$owner,and I love it" }println(fruits)println(ownFruits)//输出[Apple, Banana, Orange, Lemon][I have Apple,and I love it, I have Banana,and I love it, I have...
Kotlin基础 - infix关键字
infix关键字适用于有单个参数的扩展和类函数,可以让你以更简洁的语法调用函数,如果一个函数定义使用了infix关键字,那么调用它时,接受者和函数之间的点操作以及参数的一对括号都可以不要。 参考 public infix fun <A, B> A.to(that: B): Pair<A, B> = Pair(this, that)//使用 mapOf("Lee" to "23") 自定义一个infix函数 infix fun String?.printWithDefault(default: String) { println(this ?: default)}//使用以下两种方法都可以fun main() { "hfer".printWithDefault("hello") "we" printWithDefault "me"}
in和out
Kotlin - in和out子类泛型对象可以赋值给父类泛型对象,用out 父类泛型对象可以赋值给子类泛型对象,用in
Retrofit: 自定义注解
场景:在网络请求中,有些接口需要添加token认证,有些接口不需要 自定义注解类 @Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface Token { boolean value() default true;} 在Interceptor拦截器中实现 Java: @Overridepublic Response intercept(Chain chain) throws IOException { Request original = chain.request(); // 重新进行build Request.Builder builder = original.newBuilder(); final Token annotation =...
Kotlin Retrifit解析数据包含enum类型
在项目中经常会遇到服务端返回的字段是 int类型,用来区分不同的状态,但是前端如果直接写 if( xx == 1) else if( xx == 2 )这样的判断,代码格式不美观,而且也容易出错,因此可以在定义数据结构的时候可以增加enum类型来判断 状态 Json数据 { "id":100101, "status":1, "url":"http://www.baidu.com"} 数据结构: data class TaskResponse( val id: Int, val url: String, var photoStatus: PhotoStatus)enum class PhotoStatus(val key: Int,val valueText: String) { @SerializedName("0") //注意这里的序列化必须添加 ...