Reputation: 21
Im Current developing android application opens a new connection for every single
request/response
pair using HTTP 1.1 and async task
, but now it need to change for supporting persistent connection using GET method
, whenever server sends information so that can catch it like listener.
Im currently stuck into this, its there some good reference http persistent connection on android?
Upvotes: 1
Views: 1570
Reputation: 10278
I will answer the question you didn't ask - are persistent connections a good idea? Generally, no - not at all. There are several reasons. And I'm sure you don't actually mean HTTP (as it is designed to be stateless), you would actually want a TCP/IP port opened.
First, battery drain. TCP connections are resource intensive - even the creation/tear down of an HTTP connection is generally better than an open socket.
Second, HTTP doesn't require this. Your use-case may be different, but then maybe you shouldn't be using HTTP. Maybe XMPP or another "long polling" type solution?
Third, you should be warned that carriers like to change IP addresses on a frequent basis. So while the next response may encourage you, real-world testing and use may not meet your expectations...
Last, you can modify the "timeout" of a request, and thereby keep the connection "open" until the server responds. That is "long polling" - in a sense. You tell the server you are expecting a response but it might take a while (like a timeout of 5 or 10 minutes), but in the mean time, the carrier changes your IP address or the user moves to another tower... it happens.
If these don't apply to you, please be more specific with your use-case or scenario.
Upvotes: 2