Answer the question
In order to leave comments, you need to log in
How to implement the correct construction of the Internet connection in Android applications?
I'll start from afar. I started learning java in order to write a program for counting the number of ampoules used in one room (I did it as a calculator, I didn’t start reinventing the wheel).
Colleagues asked to make an internal chat (I don’t know why they don’t like Viber or What’sApp), but the fact is there is a fact.
I learned how to set up an Internet connection (socket). I thought now I’ll zafigachi into the application and go to drink tea. But then I decided to look at its implementation on Android.
Internet connection, as it turned out, should be done in a separate thread.
A bunch of questions immediately rained down:
How to implement flows correctly? In which thread, what should be (what should be in the main, in the children)?
How to ping the server (I understand there is no ping method in java)?
How to make a server accept multiple connections (1 server and 5 clients on the same port)?
PS If there is Russian literature, I will be glad, otherwise my knowledge is at the level of Google translator.
PPS I teach Java using the book "The Complete Guide" by Herbert Schildt.
Answer the question
In order to leave comments, you need to log in
It is advisable to create a separate topic for each question.
1. To work with the Internet, you must always use NOT the main thread, otherwise an exception will pop up. Android recommends Loader , but there are more options: AsyncTask, Service with all the work in it. All work with the UI is done in the main thread, to perform an action there (to change the UI component), use the Handler class, the runOnUiThread () method or onSuccess in AsyncTask
2. There is no such thing as a ping, but there are solutions as in the documentation , taken from here
3. Question already by the server, not by Android, ask in another category.
A generally useful source is startandroid.ru/ru/uroki.html and android documentation
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question