Kotlin协程

给类标记为挂起点使用的注解是suspend。通过反射的方式我们得知,这个suspend标记的方法的返回值是Continuation。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
interface ApiInterFace {
// 下载文件
@Streaming
@GET
suspend fun downloadFile(@Url fileUrl: String?): Response<ResponseBody>
}
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.lang.reflect.Proxy
fun main() {
var s: ApiInterFace = Proxy.newProxyInstance(
ApiInterFace::class.java.classLoader, arrayOf(ApiInterFace::class.java)
) { proxy, method, args ->
var typeArray = method.genericParameterTypes
for (value in typeArray) {
System.out.println(value)
}
""
} as ApiInterFace
runBlocking {
GlobalScope.launch {
s.downloadFileWithDynamicUrlAsync("Das")
}
delay(1000)
}
}
// 执行结果:
// class java.lang.String
// kotlin.coroutines.Continuation<? super java.lang.String>