kotlin error Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

64 Views Asked by At

error: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

I'm trying to communicate API using retrofit2, but I keep getting errors. But that error doesn't keep popping up, but the error doesn't appear once it succeeds to communicate.

val gson = GsonBuilder().setLenient().create()
val retrofit = Retrofit.Builder()
    .baseUrl("BASE_URL")
    .addConverterFactory(ScalarsConverterFactory.create()) 
    .addConverterFactory(GsonConverterFactory.create(gson)) 
    .build()
val aService:AService = retrofit.create(AService::class.java)
val apiKey = BuildConfig.MY_KEY
val aCall = aService.getInfo(apiKey, "1","50",aName,"")
if (dataList.isNotEmpty()) {
    dataList.clear()
}
Log.d("API_Request", "URL: ${aCall.request().url}")
aCall.enqueue(object : Callback<AInfo>{
    override fun onResponse(call: Call<AInfo>, response: Response<AInfo>) {
        val data = response.body()
        val aList = data?.body?.items
        if (!aList.isNullOrEmpty()) {
            aList.let { info ->
                info.forEach {
                    dataList.add(it)
                }
            }
            adapter.notifyDataSetChanged()
        }
    }
    override fun onFailure(call: Call<AInfo>, t: Throwable) {
        Log.e("API_Request_Failure", "Error: ${t.message}", t)
    }
})

When I used Log.d ("API_Request", "URL: ${aCall.request().url}") to compare URL values when communication was successful and unsuccessful. Values were the same.

And I tried to compare the response values using the onResponse function, but I couldn't check the response values because it goes to the onFailure function if an error occurs. What should I do in this case?

AService code:

interface AService {
    @GET("end_point")
    fun getInfo(
        @Query("serviceKey") serviceKey : String,
        @Query("pageNo") pageNo : String, 
        @Query("numOfRows") numOfRows : String,
        @Query("itemName") itemName : String,
        @Query("efcyQesitm") efcyQesitm : String,
        @Query("type") type: String = "json"
    ): Call<AInfo>
}

AInfo code:

import com.google.gson.annotations.SerializedName

data class AInfo(
    @SerializedName("body")
    val body: Body?,
    @SerializedName("header")
    val header: Header?
)
data class Header(
    @SerializedName("resultCode")
    val resultCode: String?,
    @SerializedName("resultMsg")
    val resultMsg: String?
)
data class Body(
    @SerializedName("items")
    val items: List<Item?>?,
    @SerializedName("numOfRows")
    val numOfRows: Int?,
    @SerializedName("pageNo")
    val pageNo: Int?,
    @SerializedName("totalCount")
    val totalCount: Int?
)
data class Item(
    @SerializedName("atpnQesitm")
    val atpnQesitm: String?,
    @SerializedName("atpnWarnQesitm")
    val atpnWarnQesitm: Any?,
    @SerializedName("bizrno")
    val bizrno: String?,
    @SerializedName("depositMethodQesitm")
    val depositMethodQesitm: String?,
    @SerializedName("efcyQesitm")
    val efcyQesitm: String?,
    @SerializedName("entpName")
    val entpName: String?,
    @SerializedName("intrcQesitm")
    val intrcQesitm: String?,
    @SerializedName("itemImage")
    val itemImage: String?,
    @SerializedName("itemName")
    val itemName: String?,
    @SerializedName("itemSeq")
    val itemSeq: String?,
    @SerializedName("openDe")
    val openDe: String?,
    @SerializedName("seQesitm")
    val seQesitm: String?,
    @SerializedName("updateDe")
    val updateDe: String?,
    @SerializedName("useMethodQesitm")
    val useMethodQesitm: String?
)
0

There are 0 best solutions below