-1

Before ask my question, I want to let you know what stage I am on. I have already implemented TCP/IP socket on my android app, it works fine(so far...).The connection between client(my android app) side and server side is short connection which is when a user submit information, a new thread will be created to send the message out, on the server side, once the server got the message, the server will respond "RCVD", after that the socket will be closed and then the connection is closed. My app has a lot of interactions between user side and server side, therefore it does many connect and disconnect between clients and server, so I always worry about the socket communications will drain phone battery and the performance will be affected.

Recently I find OkHttp on github and a lot of people suggest using it. Im not quite familiar with Http, only knows it is a higher level network protocol.

Can anyone tell me which way is better? which is more efficient for exchanging data(Object/Json/String) and media(Images)? Which is more faster and which use less battery?

Many thanks.

Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125

1 Answers1

0

Basically, the comparison between Http and tcp socket is meaningless, But in your situation it really matters. As you described, in your tcp socket way, you may create new connection each time receiving new push from server, which is not that efficient, If you use OkHttp, when your client exchange message with the same server, the same tcp socket is reused each time rather than make a new one.

By the way, As for the push service, use XMPP(over tcp) may be better cause Http is not optimized for such short message exchange model(You should use some extra strategy on the server side to keep the connection from being closed), but you may have to handle some implements about XMPP server and client.

jobcrazy
  • 1,073
  • 10
  • 16
  • thanks for your reply. In my situation, the server does not push information very often, most of time the clients send data to server side or the clients requests some specific information from the server side, do you think http can handle this more efficiently? – Haifeng Zhang Jan 21 '15 at 16:10
  • http itself does not make it more efficiently. since you are always send request to a certain server, so socket reused is significant which OkHttp library implements. If you can keep the tcp connection alive between client and server. OKhttp is not necessary. – jobcrazy Jan 22 '15 at 01:38
  • my tcpip server side is always running and accepting connection, as you said the same http socket is reused. what is the difference is here? – Haifeng Zhang Jan 22 '15 at 15:03
  • socket reuse means use an existing socket connected to the server rather than create a new one – jobcrazy Jan 23 '15 at 01:38