-2

the code is follow that const val GET_USERS_URL = "https://reqres.in/api/users"

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        usersRecycler.layoutManager = LinearLayoutManager(this)
        getUsers()
    }

    //get users list using Volley
    private fun getUsers() {
        Volley.newRequestQueue(this).add(
            StringRequest(Request.Method.GET, GET_USERS_URL, Response.Listener<String> { response ->
                val users: List<User>// TODO: Parse JSON data
            }
                //set the adapter after getting the data
                usersRecycler.adapter = UserListAdapter(this@MainActivity, users)
            }, Response.ErrorListener {
                Toast.makeText(this, "Error getting data", Toast.LENGTH_LONG).show()
            })
        )
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

Try using json parser, and the dependency you would need to add into your POM is:

<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

Then you can parse it as follows:

JSONParser parser = new JSONParser();
JSONObject obj = null;
try {
    obj = (JSONObject) parser.parse(object));
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
LHCHIN
  • 3,679
  • 2
  • 16
  • 34