我试图学习如何在Kotlin编程语言中实现Retrofit2和Moshi。但是,我似乎无法尝试编译代码。
我定义了以下数据类/模型,它们映射到我从我正在点击的api收到的json响应:
@JsonClass(generateAdapter = true)
data class Catalogs(
val languages: List<LanguageCatalog>
)
@JsonClass(generateAdapter = true)
data class LanguageCatalog(
val direction: String,
val identifier: String,
val title: String,
val resources: List<ResourceCatalog>
)
@JsonClass(generateAdapter = true)
data class Project(
val identifier: String,
val sort: Int,
val title: String,
val versification: String?
)
@JsonClass(generateAdapter = true)
data class ResourceCatalog(
val identifier: String,
val modified: String,
val projects: List<Project>,
val title: String,
val version: String
)
然后我有一个界面来定义API的行为:
interface Door43Service
{
@GET("v3/catalog.json")
fun getFormat() : Observable<Catalogs>
companion object
{
fun create(): Door43Service
{
val retrofit = Retrofit.Builder()
.addCallAdapterFactory(
RxJava2CallAdapterFactory.create()
)
.addConverterFactory(
MoshiConverterFactory.create()
)
.baseUrl("https://api.door32.org/")
.build()
return retrofit.create(Door43Service::class.java)
}
}
}
最后,我在main函数中实现了所有内容,以便从api中获取json数据:
val door43Service by lazy {
Door43Service.create()
}
var disposable: Disposable? = null
fun main(args: Array<String>)
{
door43Service.getFormat()
.subscribe(
{ result -> println(result.languages)},
{ error -> println(error.message)}
)
}
从api返回的数据很长,但可以在http://api-info.readthedocs.io/en/latest/door43.html
找到它的示例我的问题是我的堆栈中出现以下错误:
Exception in thread "main" java.lang.IllegalArgumentException: Unable to create converter for class model.Catalogs
for method Door43Service.getFormat
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:755)
at retrofit2.ServiceMethod$Builder.createResponseConverter(ServiceMethod.java:741)
at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:172)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)
at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
at com.sun.proxy.$Proxy0.getFormat(Unknown Source)
at MainKt.main(main.kt:11)
Caused by: java.lang.RuntimeException: Failed to find the generated JsonAdapter class for class model.Catalogs
at com.squareup.moshi.StandardJsonAdapters.generatedAdapter(StandardJsonAdapters.java:249)
at com.squareup.moshi.StandardJsonAdapters$1.create(StandardJsonAdapters.java:62)
at com.squareup.moshi.Moshi.adapter(Moshi.java:130)
at retrofit2.converter.moshi.MoshiConverterFactory.responseBodyConverter(MoshiConverterFactory.java:91)
at retrofit2.Retrofit.nextResponseBodyConverter(Retrofit.java:330)
at retrofit2.Retrofit.responseBodyConverter(Retrofit.java:313)
at retrofit2.ServiceMethod$Builder.createResponseConverter(ServiceMethod.java:739)
... 5 more
Caused by: java.lang.ClassNotFoundException: model.CatalogsJsonAdapter
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at com.squareup.moshi.StandardJsonAdapters.generatedAdapter(StandardJsonAdapters.java:236)
乍一看,我的理解是编译器认为我还没有为我的Catalogs类定义适配器,但我认为应该使用@JsonClass(generateradapter = true)
注释覆盖它。我有什么遗失的吗?为什么我的程序不能为我的Catalogs类生成适配器?
答案 0 :(得分:9)
所以我把事情搞定了。我所要做的就是通过在终端中运行gradle build来重建项目(我最初在IntelliJ中运行它,但实际上并没有运行构建)。关键问题是因为构建没有运行,我的gradle构建脚本中的行说,
kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version"
没有运行。这一行基本上使编译器能够理解代码中的注释,如@JsonClass。如果没有这一行,编译器将无法理解注释。这是我的问题的根本原因。如果有人遇到同样的问题,我会保留这篇文章。