Я пытаюсь установить переменную JSON, но в конце я получаю нулевой указатель. Любые идеи, как это исправить ? еще новичок с андроидом
class Requester(activity: Activity) {
...
var json : Info? = null
...
fun getData (counter : Int) {
println("frist attemp"+json)// here it is null as it should be
var show = 5*counter
var url = "https://reqres.in/api/users?page=1&per_page=$show"
var request = Request.Builder().url(url).build()
val client = OkHttpClient()
client.newCall(request).enqueue(object: Callback {
override fun onResponse(call: Call?, response: Response?) {
val body = response?.body()?.string()
val gson = GsonBuilder().create()
json = gson.fromJson(body,Info::class.java)//it should be init here
activity.runOnUiThread {
dataSize = json!!.total
val adapter = UsersAdapter(activity ,json!!,activity)
if(activity is MainActivity)
activity.recycler_View.adapter = adapter
}
isLoading=false
toast.cancel()
}
override fun onFailure(call: Call?, e: IOException?) {
activity.runOnUiThread {
val adapter = UsersAdapter(activity ,Info(0,0,0,0,
listOf(Data(0,"NO CONECTION","",""))),activity)
activity.recycler_View.adapter = adapter
Toast.makeText(activity,"Failed to connect", Toast.LENGTH_SHORT).show()
toast.cancel()
}
isLoading=false
}
})
println("end "+json)// here i get NULL any ideas why ?
}
}
Информация о классе:
class Info(val page: Int,
val per_page: Int,
val total: Int,
val total_pages: Int,
val data: List<Data>)
Данные класса:
class Data(val id: Int,
val first_name: String,
val last_name: String,
val avatar: String)
Класс UserActivity:
class UserActivity : AppCompatActivity(){
var id : Int =0
val requester : Requester = Requester(this)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_user)
id = intent.getIntExtra("id",0)
display()
}
private fun display() {
textView_ID_DEFINED.setText("$id")
requester.getData(id/3+1)
var info : Info? = requester.json
println("requester="+requester)
println("requester.info= "+requester.json)
if (info !=null){
for (i in 0..info.data.size){
if (id == info.data[i].id){
println("ahoj")
}
}
}
}
}
StackTrace:
I / Хронология: Хронология: Activity_launch_request ID: com.example.android.zadanie время: 197846234
I / System.out: requester=com.example.android.zadanie.Requester@127852b
requestter.info = null
I / Хронология: Хронология: Идентификатор Activity_idle: android.os.BinderProxy@5994e58 время: 197846419
I / System.out: mid com.example.android.zadanie.Info@e380f1b