In my activity i am calling a url using httpurl connection.
In the case when the network strength is very low, i want to disconnect that request.
How can i disconnect an httpurl connection request that is already been sent?
I want to cancel the request when back button is pressed.
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 = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(httpGet);
TIMEOUT IS BASICALLY FOR SLOW CONNECTIONS. After that much time your connection will get disconnected
Related
I am using below code to check the connection time out in Android. On top of this how can I show Toast if connection actually times out? Any suggestion?
/*Client timeout*/
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 = 3000; //3Seconds
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000; //5Seconds
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
/*Client timeout ends*/
final HttpClient client = new DefaultHttpClient(httpParameters);
**********************
//HOW TO SHOW TOAST MESSAGE WHEN THIS CASE ACTUALLY OCCURS ??
You can use try catch block for that.
try{
//your code of making request
}
catch (ConnectTimeoutException e) {
Toast.makeText(context, "Connection timed out.", Toast.LENGTH_SHORT).show();
}
Make sure to pass proper context from Activity.
Hope this helps.
I know volley have a Retry Policy,but what i know , this is for a socket timeout,not for a connection timeout, Apache HttpClient have setConnectionTimeout and setSoTimeout Method,is anyone know if i want to set connection timeout for volley framework.
you must open the HttpClientStack under package com.android.volley.toolbox; and then at the body of function performRequest you can change the values of
HttpConnectionParams.setConnectionTimeout(httpParams, your time);
HttpConnectionParams.setSoTimeout(httpParams, your time);
hope it helps.
If you want to set the Parameters of any existing HTTPClient (e.g. DefaultHttpClient or AndroidHttpClient) you can use setParams().
HttpGet httpGet = new HttpGet(url);
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(httpGet);
// after this set the parameters
httpClient.setParams(httpParameters);
Refer this:
How to set HttpResponse timeout for Android in Java
I am using url.openstream for requesting on to server. In case no internet connection while this, I want data to be stored in database, and hence doing the storage in catch clause of IOException, but instead of being caught here, it just hangs on url.openstream.
I even waited for a minute but, still it didn't get caught in IOException catch clause.
What must I do to overcome this problem?
The problem got solved by using the following method, instead of using url.openstream.
public HttpResponse getResp(String request) throws IOException
{
HttpGet httpGet = new HttpGet(request);
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 = 30000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 40000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(httpGet);
return response;
}
I have created an Android application where I connect to a remote server php file to retrieve some information. Below is the code for that.
Here I want to add timeout with the connection such as the connection will timeout in 5 seconds.
Any idea how to do this.
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name","test"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mysite.com/test.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Regards,
Shankar
Use the HttpConnectionParams of your DefaultHttpClient::
final HttpParams httpParameters = yourHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, connectionTimeOutSec * 1000);
HttpConnectionParams.setSoTimeout (httpParameters, socketTimeoutSec * 1000);
final HttpParams httpParameters = yourHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, connectionTimeOutSec * 1000);
HttpConnectionParams.setSoTimeout (httpParameters, socketTimeoutSec * 1000);
If that does not work (as in my case). try this which works for me (link)
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 = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
Kindly provide me the code for handling the HttpConnection or related Exceptions & how to display that to the user.
Specifically , I would like to know how to handle TimeOut for HttpConnection & how to display that alert to the user.
Kindly provide the same code.
Here's the code
HttpPost hPost = new HttpPost(url);
StringEntity se = new StringEntity(envelope,HTTP.UTF_8);
hPost.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(hPost);
HttpEntity entity = httpResponse.getEntity();
return entity;