提问人:Zares 提问时间:11/6/2023 更新时间:11/6/2023 访问量:33
我的 API 调用不起作用,我的应用程序返回我“预期BEGIN_ARRAY,但在路径 $ 解决方案中BEGIN_OBJECT”
My API call isn't working and my app return me "Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $ solution"
问:
嗨,我目前正在为一个班级开发一个 android 应用程序 (android studio),我需要进行 API 调用,我遵循了一些教程(android 开发人员和 YT 上的一些教程)。从这些教程中,我学会了使用 retrofit2 发出 API 请求,但我不明白为什么我收到这个错误 预期BEGIN_ARRAY,但在路径 $ 解决方案中BEGIN_OBJECT,似乎我没有收到 Json 列表,但只有一个对象,这是我用来进行调用的对象:
我试图更改对象的 Dataclass,但它不起作用,这里是我的参考代码
这是我的 ApiService 类
private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl(BASE_URL)
.build()
interface WebtoonApiService {
//Fonction qui renvoie le JSON de la page d'accueil, les deux headers son
// les clé d'accées à l'API et l'adresse de l'host
@Headers("X-RapidAPI-Key: MyKey", "X-RapidAPI-Host: webtoon.p.rapidapi.com")
@GET("titles/list?genre=ALL&sortOrder=READ_COUNT&startIndex=0&pageSize=1&language=en")
suspend fun getHome(): List<Titles>
@GET("titles/get-info?titleNo=300138&language=en")
suspend fun getTitle(): String
}
object WebtoonApi {
val retrofitService : WebtoonApiService by lazy {
retrofit.create(WebtoonApiService::class.java) }
}
My Data 类
package com.example.myapplication.network
import com.squareup.moshi.Json
data class Titles (
val message: List<String>,
val type: String,
val service: String,
val result: List<String>
)
进行 API 调用的视图模型
class HomeViewModel : ViewModel(){
init {
getHome()
}
val _status = MutableLiveData<String>()
val status: LiveData<String> = _status
private fun getHome(){
viewModelScope.launch {
try{
val currentText = WebtoonApi.retrofitService.getHome()
Log.d("TEST",currentText.toString())
_status.value = "Success: $currentText"
}
catch (e: Exception){
_status.value = "Failure: ${e.message}"
}
}
}
}
我从 GET 中得到的 Json
{
"message": {
"type": "response",
"service": "com.naver.webtoon",
"version": "0.0.1",
"result": {
"titleInfo": {
"titleNo": 300138,
"language": "en",
"writingAuthorName": "s0s2",
"writingCommunityAuthorId": "o6ocb",
"representGenre": "COMEDY",
"genreInfo": {
"name": "Comedy",
"mask": "https://webtoons-static.pstatic.net/image/genre/challenge_new_mask/mask_and_comedy.png",
"index": 0,
"color": "EEA800",
"code": "COMEDY"
},
"title": "The Little Trashmaid",
"synopsis": "Short comic strips of a mermaid in the modern days~\n\nUpdates every two weeks (on friday)\n\n\nTwitter: @s0s2\nTumblr: s0s2\nInstagram: s0s2tagram\nYoutube: s0s2",
"serviceStatus": "SERVICE",
"thumbnail": "/20210512_221/1620803524068sbgtd_JPEG/7b9a0504-bebd-4795-92db-5aa0d8659677.jpg",
"starScoreAverage": 9.79,
"readCount": 213553057,
"favoriteCount": 1481152,
"totalServiceEpisodeCount": 131,
"lastEpisodeRegisterYmdt": 1697824801000,
"linkUrl": "https://www.webtoons.com/en/canvas/the-little-trashmaid/list?title_no=300138",
"firstEpisodeNo": 1,
"genreColor": "EEA800",
"badgeType": "FAVORITE",
"likeitCount": 14929324,
"ageGradeNotice": false,
"rewardAdExposureDays": 14,
"previewDisabled": false
},
"challengeAuthorPatreon": {
"userId": "21805004",
"userName": "thelittletrashmaid",
"patronUrl": "https://www.patreon.com/bePatron?utm_source=webtoons&utm_medium=link&utm_campaign=thelittletrashmaid&u=21805004&redirect_uri=http%3A%2F%2Fm.webtoons.com%2Fapp%2Fchallenge%2FpatreonCallback%3FpageType%3DepisodeList%26title_no%3D300138",
"usePatronage": true
}
}
}
}
我的错误消息 retuen 语句当然失败了
我已经整晚都在寻找解决方案(这是我在这里的第一个问题,我尽力把它说清楚,如果不是,对不起,英语不是我的主要语言)
答:
0赞
Anna Andreeva Rogotulka
11/6/2023
#1
似乎您的响应类型和 Titles 数据类与服务的实际响应不匹配。
您需要返回单个 Titles 对象,而不是 List from 方法。WebtoonApiService.getHome
WebtoonApiService.getHome
此外,Titles 类中的字段集应与 json 中的字段匹配。例如,字段“message”应引用另一个数据类(即数据类 Message),而不是“List”。该新数据类应包含字符串字段“type”和“service”,以及另一个类型的字段“result”,即 Result(不是 List)。
评论