how to make volley request inside function in jetpack comopse

Viewed 294

I would like to send a request volley connection to this api below to get this online data to make a for loop inside getAllData function, so how can i do

code below

the api: https://adeega.xisaabso.online/android.php

Person.kt model

 package com.example.first_jetpackcompose_app.model

 data class Person(
  val id: Int,
  val firstName: String,
  val lastName: String,
  val description: String,
  val age: Int
 )

PersonRepository.kt model

package com.example.first_jetpackcompose_app.repository

import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.core.content.ContentProviderCompat.requireContext
import com.android.volley.Request
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.example.first_jetpackcompose_app.MainActivity
import com.example.first_jetpackcompose_app.model.Person
import java.security.AccessController.getContext as getContext1



class PersonRepository {
fun getAllData(): List<Person> {


// Instantiate the RequestQueue.

val queue = Volley.newRequestQueue(this)
val url = "https://adeega.xisaabso.online/android.php"

val stringRequest = StringRequest(
  Request.Method.GET, url,
  { response ->
    //textView.text = "Response is: ${response.substring(0, 500)}"
    Toast.makeText(this, "Response is: ${response.substring(0, 500)}", Toast.LENGTH_SHORT).show()
  },
  {
    Toast.makeText(this, "That didn't work!", Toast.LENGTH_SHORT).show()
    //textView.text = "That didn't work!"
  })
queue.add(stringRequest)



return listOf(
  Person(
    id = 0,
    firstName = "John",
    lastName = "Doe",
    description = "i'm learning how to request volley connection",
    age = 20
  )

)

}
}
0 Answers
Related