I have a search button which accesses a server every time I press it. The problem is that if I press it multiple times in a row, it will work fine. However, if I wait 5 seconds, the first press won't work. It just doesn't receive a response. The second press will work and so will the subsequent button presses until I wait a certain amount of time (~5 seconds)
So basically, unless you're constantly searching for new things within 5 seconds, your first button press won't do anything.
This is solved with the
System.setProperty("http.keepAlive", "false");
However, I've read that this gets rid of persistent connections where a new TCP connection has to be made every time I click that button. This is slower but it's not buggy.
Is this a bad fix? I've read that this doesn't really fix anything. Would love some help, thanks.
You said:
However, if I wait 5 seconds, the first press won't work. It just doesn't receive a response.
I think the server does not even receive a request in your case. I think setting the http.keepAlive to false is a good fix. I have spent a fair amount of time investigating this issue and it is more than obvious that this is a bug in Android library.
The reason is that the server does not want to keep all those connections open due to potentially large number of them so it closes some of them from time to time. However, the connection pool used in Android library does not want to accept this fact and tries using the old closed connection anyway.
Unsetting the http.keepAlive is a workaround rather than a solution, however you do get a reliable HTTP handling, albeit with some performance cost.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With