I have this static function which basicly makes a connection to a webpage, send post data along with it and return the received response (JSON object)
The problem that I am having is that no matter what timeout i set, it very often gives a timeout when it is only trying 1 second, with the timeout being 6 seconds that should not happen.
public static String makeRequest(String path, String info) throws Exception
{
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 6000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 6000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httpost = new HttpPost(path);
StringEntity jsonobj = new StringEntity(info);
httpost.setEntity(jsonobj);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
ResponseHandler responseHandler = new BasicResponseHandler();
String response = httpclient.execute(httpost, responseHandler);
return response;
Now I have seen some issues like this but I could not find an answer that could help me.
Some say it is due to the fact that it is not threadsafe, however, I do not do multiple calls at the same time, it is all done in order. The issue evens occurs on the first try, which should not happen because of this reason as there are no multiple connection/httpposts yet, let alone from different threads.
However it does happen a lot lately, sometimes it did not occur for a few days or barely in those days.
I tried looking at the AndroidHttpClient, but it does not seem to support HttpPost, so that is not usefull either (or I am wrong with this?)
The data for both path and info is correct, tested it. Also my server does not have issues.
Some say it can be your network, but I have it on 3 wifi networks tested today. Strangly, on the internet connection of my mobile provider it does not or barely occur.
I have read in one answer that it may be because of the ISP changing header information. I have tried using different values for the user-agent but that also did not work.
I hope someone can be of help and for that i would be grateful as it really frustrates me that this keeps happening.
If TCP detects a connection reject (i.e. an incoming RST) before the timeout period, it is a failure. There is no point in waiting out the timeout in this case, and it is to be expected that a retry will only fail again the same way. The timeout is for the case when there is no response.