class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getUsers()
}
/// ------------------------------------------
fun getUsers() {
// 1. Create Queue
val queue = Volley.newRequestQueue(this)
// 2. Create Request
val stringRequest = StringRequest(
Request.Method.GET, // 2.1 by providing method
"https://api.github.com/users", // 2.2 url
Response.Listener<String> { // 2.3 success listner
// 2.4 Create gson
val gson = Gson()
// 2.5 Create Array Type
val array = object : TypeToken<Array<User>>() {}.type
// 2.6 JSON Decode
var users: Array<User> = gson.fromJson(it, array)
// 2.7 use as per need.
println("Users are $users")
}, Response.ErrorListener { // 2.4 error listner
Toast.makeText(this, "Something went wrong. Try again.", Toast.LENGTH_SHORT).show()
})
// 3. add request to queue to execute.
queue.add(stringRequest)
}
/// ------------------------------------------
}