How to make a list from a GET request in Retrofit 2?

Asked

Viewed 268 times

0

My question is how to take the request JSON and put it in an Arraylist so I can inflate the Recyclerview. Thanks in advance.

2 answers

1

Adds a CoverterFactory the instantiation of Retrofit, through the addConverterFactory, and in case JSON returns an Array, you can easily map it to an Arraylist.

  • Thanks! I got it right.

0

You can convert your JSON into a Jsonobject and go capturing its attributes.

JSON

    {
     "nome": "José da Silva"
     "idade" : 30
     }

JAVA

JSONObject jsonObj= new JSONObject("seu json aqui");
String nome = jsonObj.getString("nome");
int idade = jsonObj.getInt("idade");

Exit

Log.d("App", "Nome: " + nome);//José da Silva
Log.d("App", "Idade: " + idade);//30

Reference: https://www.devmedia.com.br/trabalhando-com-json-em-java-o-pacote-org-json/25480

Another alternative would be the GSON library, in which you directly convert an Object to Json and a Json to Object. Link: http://www.vogella.com/tutorials/JavaLibrary-Gson/article.html

  • I managed to fix it. Thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.