首页 > 解决方案 > 预期 BEGIN_OBJECT 但在第 1 行第 1 列路径 $ 使用返回 onFailure 的 Retrofit2 时为 STRING

问题描述

我收到此错误 ** 应为 BEGIN_OBJECT 但在第 1 行第 1 列路径 $** 处为 STRING

下面我用于改造的 gradles


 implementation 'com.squareup.retrofit2:retrofit:2.6.0'
    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.12.0'

改造类

class RetrofitClient private constructor() {
    val myApi: Api

    companion object {
        @get:Synchronized
        var instance: RetrofitClient? = null
            get() {
                if (field == null) {
                    field = RetrofitClient()
                }
                return field
            }
            private set
    }

    init {

        val gson = GsonBuilder()
                .setLenient()
                .create()
        val interceptor = HttpLoggingInterceptor()
        interceptor.level = HttpLoggingInterceptor.Level.BODY
        val client : OkHttpClient = OkHttpClient.Builder().addInterceptor(interceptor).build()

        val retrofit: Retrofit = Retrofit.Builder().baseUrl("http://ctyf.co.in/api/")
                .client(client)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build()
        myApi = retrofit.create(Api::class.java)

    }
}

API接口

public interface Api {

    @Headers("Content-Type: application/json")
    @GET("companyvehiclelatestinfo?token=F934A0C439/")

    fun getLatLngs(): Call<ResponseLoc?>?
}

数据类

data class ResponseLoc(
    val Vehicle: List<Vehicle>
)

数据模型

data class Vehicle(
    val Angle: String,
    val Date: String,
    val Ignition: String,
    val Imei: String,
    val Lat: String,
    val Location: String,
    val Long: String,
    val Speed: String,
    val Tempr: String,
    val VehicleNo: String
)

终于到这里了

private fun getLatLngs() {

        val call: Call<ResponseLoc?>? = RetrofitClient.instance!!.myApi.getLatLngs()
        call!!.enqueue(object : Callback<ResponseLoc?> {

              override fun onResponse(call: Call<ResponseLoc?>, response: Response<ResponseLoc?>) {
                val responseLoc: List<ResponseLoc> = response.body() as List<ResponseLoc>

                //Creating an String array for the ListView
                val data = arrayOfNulls<String>(responseLoc.size)

                for (i in responseLoc.indices) {
                    data[i] = responseLoc[i].Vehicle.toString()
                    Log.d("apiii",  data[i].toString())
                }

            }

            override fun onFailure(call: Call<ResponseLoc?>, t: Throwable) {

                Log.d("apii",  t.message.toString())
            }

        })
    }

JSON 值

{"Vehicle":[{"VehicleNo":"Test","Imei":"354019","Location":"Tamil Nadu-India","Date":"2021-03-17 19:27:12.000","Tempr":"0","Ignition":"","Lat":"13.11","Long":"80.282","Speed":"0","Angle":"0"}]}

我尝试了很多堆栈,但都没有帮助

除了改造还有其他方法吗???

任何人请帮我获得api结果

我尝试了很多堆栈,但都没有帮助

除了改造还有其他方法吗???

任何人请帮我获得api结果

标签: androidapikotlinretrofitretrofit2

解决方案


推荐阅读