前提
Android Studioでkotlinを用いてAWSLambdaで作成したRestAPIと接続し、json配列を受け取りたいです。
その際に以下のようなエラーが発生しました。
実現したいこと
json配列のnumを受け取りたいです。
発生している問題・エラーメッセージ
debug com.squareup.moshi.JsonDataException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at path $.failed_list
該当のソースコード
ApiModel.kt
1val moshi = Moshi.Builder() 2 .add(KotlinJsonAdapterFactory()) 3 .build() 4 5 val retrofit = Retrofit.Builder() 6 .baseUrl("https://xxvqdh5zk1.execute-api.ap-northeast-1.amazonaws.com/") 7 .addConverterFactory(MoshiConverterFactory.create(moshi)) 8 .build() 9 10fun fetchApiFailedList():Array<String>{ 11 var q=arrayOf<String>() 12 try { 13 val service: FailedQuestionService = retrofit.create(FailedQuestionService::class.java) 14 val questionApiResponse=service.fetchFailedQuestion().execute().body() 15 ?: throw IllegalStateException("body is null") 16 q=questionApiResponse 17 Log.d("response-q",q.toString()) 18 }catch (e: Exception) { 19 Log.d("response-fAFL", "debug $e") 20 } 21 return q 22 }
TOservicel.kt
1interface FailedQuestionService{ 2 @GET("/dev/failed_list") 3 fun fetchFailedQuestion(): retrofit2.Call<Array<FailedListInfo>> 4}
TOInfo.kt
1data class FailedListInfo( 2 val failed_list: Failed 3) 4 5data class Failed( 6 val answer: String, 7 val num: Int, 8 val question: String 9) 10
json
1{ 2 "failed_list": [ 3 { 4 "num": 1, 5 "question": "In the article, Ms.Amanjit ___ her internship at a Chennai publishing company last year.", 6 "answer": "desclibed" 7 }, 8 { 9 "num": 2, 10 "question": "Please submit ___ travel-expense reports to Ms.Gresh in the finance department", 11 "answer": "your" 12 } 13 ] 14}
試したこと
TOserviceのcallをretrofit2.Call<FailedListInfo>からretrofit2.Call<Array<FailedListInfo>>へ変更
回答1件
あなたの回答
tips
プレビュー