Exception to be handled for HttpConnection - android

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;

Related

How to set connection timeout for volley?

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

Android HttpURLConnection disconnect

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

Android: Is there a way to set a timeout for response = httpclient.execute(httppost);

I'm trying to load information over the network on a thread. When there is no internet it will freeze for a long time before setting off a exception or just freeze.
Is there a way to set a timeout for // FREEZES HERE or takes a long time to through exception when there is no internet?
Is their a way to set a timeout for response = httpclient.execute(httppost);?
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://besttechsolutions.biz/projects/bookclub/getevents.php");
// FREEZES HERE or takes a long time to through exception when there is no internet
response = httpclient.execute(httppost);
responseBody = EntityUtils.toString(response.getEntity());
Try the following.
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 30000);
HttpConnectionParams.setSoTimeout(httpParameters, 30000);
if(null == httpClient)
httpClient = new DefaultHttpClient(httpParameters);
The above code generally sets a default timeout for httpClient. To check internet access, refer the link posted by Nomand.

android using url.openstream isn't throwing IOException if no internet connection. Why?

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;
}

Timeout in DefaultHttpClient Class Android

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);

Categories

Resources