S
S
Stepan2021-02-05 19:50:26
Java
Stepan, 2021-02-05 19:50:26

How to properly process JSON response from VK API and fill RecyclerView?

Hey!

I understand development for android and decided to make an application that:
- via API requests data from VK about users whose id is specified by the user,
- converts the received JSON response into an array,
- creates a RecyclerView from this array, filling it with data (id, name , last name, avatar).

What the code is doing now:
- takes the listed VK user IDs (1,2,3,4,5),
- when the button is clicked, generates a URL for a request to the VK ( https://api.vk.com/method/users.get ?user_ids=1,2,3... ),
- starts processing this URL in a separate thread: if there is data, an array is formed from them,
- when the task in a separate thread is completed, the RecyclerView is filled.

When the button is pressed for the first time, the array is not formed, despite the fact that data has been received from the VC.
If you click on the button a second time, everything works - a list of users is formed.

I sin that the necessary pieces of code are executed in the wrong places where they should be: not everything is executed in a separate thread or not as it should be. Or something that should not be executed is being executed...

I would be grateful if you could tell me how to improve the code: https://github.com/StepanMe/VKInfo
What libraries can make life easier?

Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stepan, 2021-02-06
@steff

It seems to have figured it out.
I changed the code a little + found out that the onPostExecute method did not work for me (it was not explicitly overridden via @Override). Now at the output I get the expected result - the code returns the data I need "on the first try" and displays a list of users in the RecyclerView.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question