【问题标题】:java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ errorjava.lang.IllegalStateException:应为 BEGIN_ARRAY,但在第 1 行第 1 列路径 $ 错误
【发布时间】:2020-10-19 10:33:48
【问题描述】:

我知道这个问题还有其他一些主题,但我找不到我的错误的解决方案,所以如果您有任何建议,请告诉我。我正在尝试在正文中创建一个带有参数的 POST 请求,以从 API 中检索一些数据。

我的 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>>

}

我在使用 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){
            }
        }
    }

我的 CashCollection 数据类:

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

来自服务器的响应: {"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) 

当我尝试测试和检查收到的数据时,我收到了该错误。

【问题讨论】:

    标签: android kotlin post request retrofit2


    【解决方案1】:

    请提供您的数据类别和来自服务器的响应。此错误意味着来自服务器的数据无法映射到您作为这些请求的结果提供的数据类中。

    好的,数据类必须准确反映响应的结构。你的CashCollection 类必须是

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

    以及回复类型Call&lt;CashCollection&gt;

    【讨论】:

    • 请再次检查问题,我添加了 Data Clasa 和响应。谢谢!
    • 即使我修改了那个类,我仍然遇到同样的错误..
    • 能否分享项目的完整代码。以 GitHub 为例
    • 项目很复杂,看完整代码我觉得帮不了你
    • 也许问题是我如何尝试发送 body 参数? @马克西姆
    【解决方案2】:

    您似乎错误地将您的 api 响应映射到 kotlin 类。在您的 Retrofit api 中,您说您希望 api 返回 CashCollections 列表 ([{"label":"12.12.1212", "value": ""0,00}]),但服务器返回一个 json 对象,该数组位于 data 属性中:{"data":[{"label":"12.12.1212", "value": "0,00"}]}

    您应该更新您的改造界面以对应 api 返回的内容:

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

    【讨论】:

    • 我在我的数据类中添加了@SerializedName("data") 但我仍然遇到同样的错误..
    • 您在哪个数据类中添加了@SerializedName("data")?因为您应该创建一个全新的数据类来包装数据,所以您不应该将它添加到 CashCollection 数据类中。
    • 另外,正如您所说,问题可能不在响应中,而是在请求中。我建议您阅读此内容:futurestud.io/tutorials/…。所以要么将主体定义为 RequestBody,要么使用标量转换器工厂
    • 我在接口 ChartsDataApi @Headers("{Content-Type: text/plain; charset=UTF-8}") 中添加了这一行,这解决了这个问题。但是现在我得到了“错误请求”的响应..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 2021-10-27
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多