Ответ, который я получаю
[{date_time: 2019-02-07 10:09:01, vital_code : S1, vital_value :25, created_at : 2019-02-07 04:39:09, id = 1}, {date_time: null, vital_code : S2, vital_value :150, created_at : null, id = null}]
Ошибка указывает на строку
Process: com.example.healthpassport, PID: 21563
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.get(ArrayList.java:411)
at com.example.healthpassport.activity.HealthMetrics$onCreate$1$override.onResponse(HealthMetrics.kt:31)
at com.example.healthpassport.activity.HealthMetrics$onCreate$1$override.access$dispatch(HealthMetrics.kt)
at com.example.healthpassport.activity.HealthMetrics$onCreate$1.onResponse(HealthMetrics.kt)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6780)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Так я вызываю и повторяю ответ
val call=RetrofitClient.instance.api.getVitals("Bearer "+token)
call.enqueue(object : Callback<List<VitalsResponse>> {
override fun onResponse(call: Call<List<VitalsResponse>>?, response: Response<List<VitalsResponse>>?) {
Log.e("Response", response!!.body().toString())
for (i in 0..response.body().size) {
if (response.body() != null) {
Log.e("Response", response!!.body()[i].getVital_value())
Log.e("Response", response!!.body()[i].getVital_code())
}
else{
Log.e("Response", "No Response")
}
}
}
override fun onFailure(call: Call<List<VitalsResponse>>?, t: Throwable?) {
Log.e("VitalResponse", t!!.message)
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
})
Может кто-нибудь сказать мне, в чем может быть причина, по которой я не могу понять.