简体   繁体   English

java.lang.IllegalStateException:预期为 BEGIN_ARRAY,但在第 1 行第 1 列路径 $ 错误处为 STRING

[英]java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ error

I know there are some other topics on this problem but I couldn't find the solution for my error so if you have any suggestions, please let me know.我知道这个问题还有其他一些主题,但我找不到解决我的错误的方法,所以如果您有任何建议,请告诉我。 I'm trying to create a POST request with a param in the body to retrieve some data from the API.我正在尝试创建一个在正文中带有参数的 POST 请求,以从 API 中检索一些数据。

My DataApi interface:我的 DataApi 接口:

interface ChartsDataApi {

@POST(NetworkUtils.CASH_COLLECTION_URL)
fun getCashCollection(@Body guid: String) : Call<List<CashCollection>>

@POST(NetworkUtils.TICKETS_DETAILS_URL)
fun getTicketsDetails() : Call<List<TicketDetails>>

} }

My fun where I'm using Retrofit and where I'm trying to get the response:我在使用 Retrofit 以及尝试获得响应的地方很有趣:

private fun geCashCollectionDetails(){
    var params : MutableMap<String, String> = HashMap()
    val BASE_URL = " here i wrote the base URL "
    val gson = GsonBuilder()
            .setLenient()
            .create()
   val api = Retrofit.Builder()
           .baseUrl(BASE_URL)
           .addConverterFactory(GsonConverterFactory.create(gson))
           .build()
           .create(ChartsDataApi::class.java)

    GlobalScope.launch(Dispatchers.IO){
        val response = api.getCashCollection(" **here i wrote the guid** ").awaitResponse()
        if(response.isSuccessful){
            val day = response.body()!!.lastIndex
            Log.d("RAZZ", day.toString())
            withContext(Dispatchers.Main){
            }
        }
    }

My Data Class for CashCollection:我的 CashCollection 数据类:

data class CashCollection(
    @SerializedName("label")
    val label: String?,
    @SerializedName("value")
    val value: String?)

Response from the server: {"data":[{"label":"12.10.2020","value":"0,00"},{"label":"16.10.2020","value":"0,00"},{"label":"17.10.2020","value":"0,00"},{"label":"18.10.2020","value":"0,00"}],"currency":"EUR"}来自服务器的响应: {"data":[{"label":"12.10.2020","value":"0,00"},{"label":"16.10.2020","value":"0 ,00"},{"label":"17.10.2020","value":"0,00"},{"label":"18.10.2020","value":"0,00"}], "货币":"欧元"}

Process: com.eschbachit.citylinemobile, PID: 32529
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226)
    at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
    at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
    at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:243)
    at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:153)
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:776)
 Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
    at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:215)
    at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37) 
    at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25) 
    at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:243) 
    at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:153) 
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174) 
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
    at java.lang.Thread.run(Thread.java:776) 

When I'm trying to test and check the data I'm receiving, I get that error.当我尝试测试和检查我收到的数据时,我收到了那个错误。

Please provide your data classes and response from server.请提供您的数据类和来自服务器的响应。 This error means data from server can't be mapped into data classes that you provide as result of those requests.此错误意味着来自服务器的数据无法映射到您作为这些请求的结果提供的数据类中。

Ok, data class must exactly reflect the structure of the response.好的,数据类必须准确反映响应的结构。 Your CashCollection class must be您的CashCollection类必须是

data class CashCollection(
    val data: ArrayList<CashCollectionItem>,
    val currency: String
) {
   data class CashCollectionItem(
       @SerializedName("label")
       val label: String?,
       @SerializedName("value")
       val value: String?)
}

and type of response Call<CashCollection>和响应类型Call<CashCollection>

It appears you have incorrectly mapped your api response to kotlin classes.您似乎错误地将 api 响应映射到 kotlin 类。 In your Retrofit api you say you expect the api to return a list of CashCollections ( [{"label":"12.12.1212", "value": ""0,00}] ), but the server returns a json object where that array is in the data property: {"data":[{"label":"12.12.1212", "value": "0,00"}]}在您的 Retrofit api 中,您说您希望 api 返回 CashCollections 列表( [{"label":"12.12.1212", "value": ""0,00}] ),但服务器返回一个 json 对象,其中该数组位于数据属性中: {"data":[{"label":"12.12.1212", "value": "0,00"}]}

You should update your retrofit interface to correspond to what the api returns:您应该更新改造接口以对应 api 返回的内容:

class CashCollectionResponse(
  @SerializedName("data") data: List<CashCollection>
)

interface ChartsDataApi {
  @POST("GetTheData")
  fun getCashCollection(@Body guid: String): Call<CashCollectionResponse>
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复错误:java.lang.IllegalStateException:应为BEGIN_ARRAY,但在第1行第1列路径$处为STRING - How to fix Error:java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ 获取错误 java.lang.IllegalStateException: 预期为 BEGIN_OBJECT 但在第 1 行第 2 列路径处为 BEGIN_ARRAY - Getting error java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ Gradle Build失败,出现java.lang.IllegalStateException:预期为BEGIN_ARRAY但是STRING在第1行第1列路径$ - Gradle Build failing with java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ java.lang.IllegalStateException:应为 BEGIN_ARRAY,但在第 1 行第 1 列路径 $ 处为 STRING - java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ 改造 Android:java.lang.IllegalStateException:预期为 BEGIN_ARRAY,但在第 1 行第 2 列路径为 BEGIN_OBJECT - Retrofit Android : java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path Retrofit2 java.lang.IllegalStateException:预期为 BEGIN_ARRAY,但在第 1 行第 2 列路径 $ 处为 BEGIN_OBJECT - Retrofit2 java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ java.lang.IllegalStateException:应为BEGIN_OBJECT,但在第1行第2列$处为BEGIN_ARRAY - java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ Andorid Studio java.lang.IllegalStateException:应为 BEGIN_ARRAY,但在第 1 行第 2 列路径为 BEGIN_OBJECT - Andorid Studio java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ RetroFit:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第2列路径$处为BEGIN_ARRAY - RetroFit: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ 改造-&gt; java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在第1行第2列路径$ BEGIN_OBJECT - Retrofit --> java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM