android - 预期 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结果
解决方案
推荐阅读
- ruby-on-rails - 如何更改 RSpec 的存根
- python - 熊猫,str.extract 不适用于我的正则表达式?
- c - linux malloc:何时实际映射页面?
- r - R - 以 1 为增量的循环函数
- android - 如果应用程序关闭,警报管理器不会触发(从最近的应用程序中删除)
- python - selenium.common.exceptions.ElementNotInteractableException:消息:元素不可交互尝试发送键
- linux - 如何比较两个文件并在第一个文件的位置替换另一个文件内容?
- mongodb - MongoDB 使用 $text 和其他查询参数
- python - QQmlPropertyMap 插入;神秘地插入无或删除其他值
- flutter - 如何在 Flutter 中使容器模糊?