J
J
jokkie2017-10-16 19:16:07
Android
jokkie, 2017-10-16 19:16:07

Android communication with server, Socket or httpurlconnection?

I am developing an Android client-server application, in which data will be rarely transmitted from the server-> client, but there will be a lot of them, little data will be transmitted from the client side, but they will be transmitted much more often (preferably once every 5 seconds, but can be increased to 10 -15 sec), interaction, mainly in the mobile network, i.e. connection is not stable, may be interrupted.
I found the Socket and httpurlconnection mechanisms, but somehow I didn’t google comparisons between them, Socket, as I understand it, keep a stable connection, and httpurlconnection connects “once” and for a normal exchange you need to do short or long polling, i.e. at first glance, it's better to use Socket, but on the net I see implementations with httpurlconnection more often.
Why don't they use sockets or what do I not understand about using sockets on Android?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
retr0, 2017-10-16
@retr0

Socket and HttpUrlConnection are long outdated libraries. Google recommends using OkHttp. You can see an example of usage here

S
Sergey, 2017-10-16
@red-barbarian

Well, first you need to decide on the protocol. will you make your own client-server exchange protocol or will you take http.
http is already a standard protocol with all the consequences. and a good client for android.
your protocol - all your desires. but do it yourself.
besides this, you need to take into account that ready-made http clients are already ready for compressed data, for working with REST. for cache. For them, you can immediately work with Retrofitom & json (Gson), or parse it yourself.
Of course, there are ready-made servers for http. it remains to make only answers-scripts.
In addition, often in any corporate network, the administrator prohibits everything except mail & http.
Those. there are quite a few pluses.
But for http, this is the work of the client with the server on request. those. the client sent a request - received a response. The server just doesn't send its data to the client.
If this is important to you, then http will not work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question