如何修复 kotlin 中的错误 GET 请求 API?
How to fix an error GET request API in kotlin?
我在瑞克和莫蒂中发起请求时遇到问题 API。
我尝试在调试中启动应用程序,但出现错误 404
我不明白为什么我会收到 404 错误,因为基数 URL 是正确的。
谢谢你的帮助!!
class MainActivity : AppCompatActivity() {
private val url = "https://rickandmortyapi.com/api/"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val retrofit = Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(MoshiConverterFactory.create())
.build()
val service = retrofit.create(ApiService::class.java)
val characterRequest = service.listCharacters()
characterRequest.enqueue(object : Callback<List<Character>> {
override fun onResponse(call: Call<List<Character>>, response: Response<List<Character>>
) {
val allCharacters = response.body()
if (allCharacters != null) {
Log.i("Success","HERE is ALL CHARACTERS FROM RICK AND MORTY:")
for (c in allCharacters)
Log.i("Success"," one character : ${c.name} : ${c.gender} ")
}
else (
Log.i("Error","Empty ")
)
}
override fun onFailure(call: Call<List<Character>>, t: Throwable) {
Log.e("Error", "KO")
}
})
API服务:
interface ApiService {
@GET("character")
fun listCharacters(): Call<List<Character>>
}
字符class:
class Character(val name : String , val gender : String) {
}
我的依赖项:
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.retrofit2:converter-moshi:2.9.0"
implementation "com.squareup.okhttp3:okhttp:4.9.3"
}
我想错误出在您定义 listCharacters
方法的响应类型的方式上。响应类型不应是您定义的 List<Character>
。尝试下一个方法:
interface ApiService {
@GET("character")
fun listCharacters(): Call<ApiResponse>
}
data class ApiResponse(val results: List<Character>)
characterRequest.enqueue(object : Callback<ApiResponse> {
override fun onResponse(call: Call<ApiResponse>, response: Response<ApiResponse>
) {
val apiResponse: ApiResponse? = response.body()
// use apiResponse
}
override fun onFailure(call: Call<ApiResponse>, t: Throwable) {
}
})
我在瑞克和莫蒂中发起请求时遇到问题 API。 我尝试在调试中启动应用程序,但出现错误 404 我不明白为什么我会收到 404 错误,因为基数 URL 是正确的。 谢谢你的帮助!!
class MainActivity : AppCompatActivity() {
private val url = "https://rickandmortyapi.com/api/"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val retrofit = Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(MoshiConverterFactory.create())
.build()
val service = retrofit.create(ApiService::class.java)
val characterRequest = service.listCharacters()
characterRequest.enqueue(object : Callback<List<Character>> {
override fun onResponse(call: Call<List<Character>>, response: Response<List<Character>>
) {
val allCharacters = response.body()
if (allCharacters != null) {
Log.i("Success","HERE is ALL CHARACTERS FROM RICK AND MORTY:")
for (c in allCharacters)
Log.i("Success"," one character : ${c.name} : ${c.gender} ")
}
else (
Log.i("Error","Empty ")
)
}
override fun onFailure(call: Call<List<Character>>, t: Throwable) {
Log.e("Error", "KO")
}
})
API服务:
interface ApiService {
@GET("character")
fun listCharacters(): Call<List<Character>>
}
字符class:
class Character(val name : String , val gender : String) {
}
我的依赖项:
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.retrofit2:converter-moshi:2.9.0"
implementation "com.squareup.okhttp3:okhttp:4.9.3"
}
我想错误出在您定义 listCharacters
方法的响应类型的方式上。响应类型不应是您定义的 List<Character>
。尝试下一个方法:
interface ApiService {
@GET("character")
fun listCharacters(): Call<ApiResponse>
}
data class ApiResponse(val results: List<Character>)
characterRequest.enqueue(object : Callback<ApiResponse> {
override fun onResponse(call: Call<ApiResponse>, response: Response<ApiResponse>
) {
val apiResponse: ApiResponse? = response.body()
// use apiResponse
}
override fun onFailure(call: Call<ApiResponse>, t: Throwable) {
}
})