我第一次尝试使用retrofit2。我在网上阅读了很多教程并阅读了文档。我理解这些步骤并且没想到会出现任何错误;)但是当我运行该应用程序时,出现以下错误:
Caused by: java.lang.IllegalArgumentException: Unable to create call adapter for
java.util.List<com.example.myretrofit.MarsPhoto>
for method MarsApiService.getPhotos
我使用了带有 noActivity 的创建应用程序,因此我的代码是基本代码,但在文件顶部添加了改造。我只是想尝试改造并启动对 api 的 get 调用。
这是使用 kotlinx.serilization 获取 json 并使用 Retrofit2 获取 http get 的 MainActivity.kt 代码:
package com.example.myretrofit
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.myretrofit.ui.theme.MyRetrofitTheme
//retrofit implementation
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import retrofit2.Retrofit
import retrofit2.http.GET
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import retrofit2.Call
import kotlinx.serialization.encodeToString
/**
* This data class defines a Mars photo which includes an ID, and the image URL.
*/
@Serializable
data class MarsPhoto(
val id: String,
@SerialName(value = "img_src")
val imgSrc: String
)
private const val BASE_URL =
"https://android-kotlin-fun-mars-server.appspot.com"
/**
* Use the Retrofit builder to build a retrofit object using a kotlinx.serialization converter
*/
private val retrofit = Retrofit.Builder()
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
.baseUrl(BASE_URL)
.build()
/**
* Retrofit service object for creating api calls
*/
interface MarsApiService {
@GET("photos")
fun getPhotos(): List<MarsPhoto>
}
/**
* A public Api object that exposes the lazy-initialized Retrofit service
*/
object MarsApi {
val retrofitService: MarsApiService by lazy {
retrofit.create(MarsApiService::class.java)
}
}
//end of retrofit implementation
val listResult = MarsApi.retrofitService.getPhotos()
// for (item in listResult) {
// Log.i("ServerItem : ", "$item")
// }
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyRetrofitTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
val listResult = MarsApi.retrofitService.getPhotos()
Log.i("ServerItem : ", "print something")
Greeting("Android")
}
}
}
}
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier
)
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
MyRetrofitTheme {
Greeting("Android")
}
}
显然我在阅读所有 Retrofit2 教程和文档时错过了一些东西。 错误提示是接口代码错误但不知道为什么?我没有适配器,实际上我也不需要带有 kotlinx.serilization 的适配器,对吗??
如何修复此错误以便我可以从 api 获取响应?
文档在这里
请记住,我在 android studio/kotlin/apps 开发中处于 0 级。我三周前才开始。我理解并能够很好地使用 compose。我了解如何使用活动和协程。我不想在这里添加 coRoutines,因为我想了解 Retrofit2,所以我可以接受阻塞线程的调用。
我相信这个错误是因为接口很有趣 getPhotos() 当我把它拿出来时,错误消失了,应用程序运行正常,但我从改造实例中没有得到任何东西。没有任何类型的日志记录。
感谢您尝试提供帮助。
如何修复此错误以便我可以从 api 获取响应?
kotlinx.serialization
并未正式作为 Retrofit 的转换器工厂提供。如果您仍想将其与 Retrofit 一起使用,则需要使用此库。
但是,我建议使用更简单的选项,例如
GsonConverter
,可用于改造。为此,首先添加依赖项:
implementation("com.squareup.retrofit2:converter-gson:<your_retrofit_version>")
然后,在创建Retrofit客户端时添加转换器工厂:
Retrofit.Builder()
.baseUrl("<your_api_base_url>")
.addConverterFactory(GsonConverterFactory.create())
.build()
有关依赖注入项目中更详细的用例,请查看this。