kotlin - Squareup Retrofit/Moshi 示例进程未关闭
问题描述
我有一个适用于 Squareup 的 Retrofit2 http 客户端 ( https://square.github.io/retrofit/ ) 的“hello world”应用程序,使用 JSON 与 Moshi ( https://github.com/square/moshi ) 解组...
一切正常,但没有System.exit(0)
应用程序将永远运行。
知道这是为什么吗?
import com.squareup.moshi.JsonClass
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.GET
import retrofit2.http.Path
fun main() {
MainApp().doIt()
println("main READY!")
System.exit(0)
}
interface GitHubService {
@GET("users/{user}/repos")
fun listRepos(@Path("user") user: String): Call<List<Repo>>
}
@JsonClass(generateAdapter = true)
data class Repo(val full_name: String, val private: Boolean)
class MainApp {
fun doIt() {
val moshi = Moshi.Builder()
.addLast(KotlinJsonAdapterFactory())
.build()
val retrofitGithub = Retrofit.Builder()
.baseUrl("https://api.github.com/")
.addConverterFactory(MoshiConverterFactory.create(moshi))
.build()
val githubService = retrofitGithub.create(GitHubService::class.java)
val reposCall: Call<List<Repo>> = githubService.listRepos("HoffiMuc")
val response = reposCall.execute()
if (response.isSuccessful) {
val repos = response.body() ?: ArrayList<Repo>()
if ( repos.isNotEmpty() ) {
for (repo in repos) {
println(repo)
}
} else {
println("Response Body is null or no repo in result list")
}
} else {
println(response.headers())
}
}
}
解决方案
如果您添加对最新版本的 OkHttp 的显式依赖会发生什么?旧版本 Retrofit 依赖于使用的非守护线程。
推荐阅读
- docker-compose - 如何在 docker-compose 中挂载我的本地目录,以便我的 Dockerfile 可以从中复制文件?
- ansible - Ansible:在本地运行一个交互式shell脚本只是运行通过
- css - 在运行 gulp watch 时,我的 vs 代码终端在启动“watch”时停止
- typescript - 如何从 tsconfig.js 中包含的父文件夹中仅排除一个文件夹
- java - 如何在 Java 中像 Python 代码一样只使用 2 个变量?
- c# - EF Core - 如何避免自定义RequiredAttribute 将数据库列设置为不可为空
- r - 从 R 中的 highchart 中抓取数据
- asp.net-web-api2 - 尚未使用 Dapper 初始化连接字符串属性以从 SQL Server 2014 数据库获取数据
- intellij-idea - 从 Eclipse 中查看 IntelliJ IDEA 2020.2(终极版)中的类似功能 / JBoss 的“清理”、“文件浏览器”和“服务器实例”
- xml - 如何在 Windows 上安装 perl XML::XPath?