Http Connection Timeout on Android Not Working

Http connection timeout on Android not working

Try to do it this way:

HttpPost httpPost = new HttpPost(url);
StringEntity se = new StringEntity(envelope,HTTP.UTF_8);
httpPost.setEntity(se);

HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 3000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
BasicHttpResponse httpResponse = (BasicHttpResponse) httpClient.execute(httpPost);

HttpEntity entity = httpResponse.getEntity();
return entity;

You then can catch a possible ConnectTimeoutException.

Android: connection timeout not working (connection waits forever)

If you have no Internet the DNS request will fail, but you cannot control the DNS timeout. Eventually you should get an UnknownHostException, but this may take several minutes. In this case the best thing to do is to start a timer in a separate thread at the same time you make the HTTP request. If the timer goes off before the HTTP request has finished, you can abort the HTTP request.

how to set Http connection timeout on Android

Try this ,private static long TIME_OUT_IN_SECONDS = 120;

          System.out.println("posthhhhhhhhhhhhhhhhhhh");

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));

HttpResponse httpResponse = null;
long requestStratTime = new Date().getTime();

httpResponse = httpClient.execute(httpPost);
long requestEndTime = new Date().getTime();
Log.d("requestStratTime", "requestStratTime" + requestStratTime);
Log.d("requestEndTime", "requestEndTime" + requestEndTime);
long timeOfRequest = (requestEndTime - requestStratTime) / 1000;
Log.d("timeOfRequest", "timeOfRequest" + timeOfRequest);
if (httpResponse == null && timeOfRequest > TIME_OUT_IN_SECONDS) {

throw new TimeOutException();
}

int responseCode = httpResponse.getStatusLine().getStatusCode();
System.out.println("responseCode" + responseCode);
String ss = httpResponse.getStatusLine().toString();
System.out.println("ssssssssssssssssssssssss" + ss);
if (responseCode == 200) {

HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

} else {
throw new ParsingException();
}

Connection Timeout Android

VollyJson library have a capability to differentiate different network errors while communicating with webservices.When something went wrong with JSONObjectRequest onErrorResponse will be called,there you can differentiate the error as follows.

public void onErrorResponse(VolleyError error) {

if (error instanceof TimeoutError || error instanceof NoConnectionError) {
//Write Your code here
} else if (error instanceof AuthFailureError) {
//TODO
} else if (error instanceof ServerError) {
//TODO
} else if (error instanceof NetworkError) {
//TODO
} else if (error instanceof ParseError) {
//TODO
}

Android socket connection timeout

After googling a lot I found out a solution to this problem. Add timeout to the socket connection.

mSocket.setSoTimeout(10*1000);

If there isn't any response, after 10 seconds it will throw SocketTimeoutException and in the catch of this exception close the connection if exists, then connect again.

catch (SocketTimeoutException e) {
if (mSocket.isConnected()) {
disconnect();
}
connect();
}

Why timeout value is not respected by android HttpURLConnection?

It appears that Java URLConnection provides no fail-safe timeout on reads

The solution is, as the article explains, to use a separate Thread for timing, and disconnect the HttpURLConnection manually once the timer thread is done.

set timeout in httprequest android

You can do it as follows:

try{     
HttpGet httpGet = new HttpGet(url);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 4000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 6000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(httpGet);
} catch (ConnectTimeoutException e) {
//Here Connection TimeOut excepion
Toast.makeText(xyz.this, "Your connection timedout", 10000).show();
}


Related Topics



Leave a reply



Submit