S
S
Stepan2021-02-10 15:02:59
Java
Stepan, 2021-02-10 15:02:59

How to properly organize code in Java?

Hello.

Tell me, how would it be more correct to organize the mobile application code?
1. Take data from the text field, make a request in a separate thread, get a response in JSON, fill the object with the received data. Next, work with the object.
2. Take data from the text field, pass it to the object, in the constructor of which there will be a "request in a separate thread", the data from the response to which the object will be replenished. Further, also continue to work with the object.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2021-02-10
@vabka

I'll write in pseudocode:

пусть значение = получить_значение_из_поля();
пусть результат = запросить(значение);

In general, spawning new threads for each request is not very good. It's better to use async. Xs what is there in Java for this, to be honest.
There is GSON for parsing the response.
If you were to use kotlin then I would suggest using ktor or http4k

O
Orkhan, 2021-02-10
Hasanly @azerphoenix

Good afternoon!
Use asynchronous requests.
For queries, use Retrofit. You can use GSON or Jackson to serialize and deserialize JSON. By the way, for asynchronism you can look at RxJava.
Thus, the first option is preferable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question