I need to know if my glass has internet connectivity.
I have tried to use this solution How to check if Google Glass is connected to internet using GDK
public static void isNetworkAvailable(Context context){
HttpGet httpGet = new HttpGet("http://www.google.com");
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);
try{
Log.d(TAG, "Checking network connection...");
httpClient.execute(httpGet);
Log.d(TAG, "Connection OK");
return;
}
catch(ClientProtocolException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
Log.d(TAG, "Connection unavailable");
}
It's work ok for my in XE 17.1 but when i have updated my Glass to XE 18.1 it's work but i need to wait until TIME_OUT to know Glass have not internet connectivity. In XE 17.1 it's not happen. If i hadn't connectivity, a exception was threw without waiting TIME_OUT.
Any idea for know internet connectivity without waiting Time out?
Thanks!
Have you tried using InetAddress.getByName("google.com").isReachable(<timeout>) for testing connectivity? It might not be as reliable as a GET request over TCP, but I've seen this work pretty well in practice.
Related
i'm stuck with a timeout problem with DefaultHttpClient on Android. I'm trying to set the timeout with the following piece of code:
HttpClient client = new DefaultHttpClient();
HttpParams httpParameters = client.getParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 4000);
HttpConnectionParams.setSoTimeout(httpParameters, 4000);
But if the device is connected to a network without internet connection, the timeout is never fired and the execution of http request never throws any timeout exception. I'm executing http request as follow:
HttpResponse httpResponse = client.execute(request);
I've tried also to set the timeout on the HttpRequest, with the following lines:
HttpRequestBase request = ...
request.setParams(httpParameters);
Android seems to ignore the timeout settings and when executing http request on a network with no internet connection, all the requests fails after about 20s, and not after my timeout settings.
I've also tried to close all internet connections and abort http request after a timeout with a parallel thread. I've used the following piece of code:
HttpClient client = new DefaultHttpClient();
HttpParams httpParameters = client.getParams();
HttpRequestBase request = ...
request.setParams(httpParameters);
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutReal);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutReal);
request.setParams(httpParameters);
((DefaultHttpClient) client).setParams(httpParameters);
Thread t = new Thread(){
public void run()
{
try
{
Thread.sleep(4000);
request.abort();
client.getConnectionManager().closeExpiredConnections();
client.getConnectionManager().closeIdleConnections(4000,TimeUnit.MILLISECONDS);
client.getConnectionManager().shutdown();
Log.i("TEST SHUTDOWN","SHUT DOWN ALL CONNECTIONS");
}
catch (InterruptedException e)
{
}
}
};
try
{
t.start();
HttpResponse httpResponse = client.execute(request);
}
catch (Exception e)
{
Log.i("TEST SHUTDOWN","EXCEPTION "+e);
}
finally
{
t.interrupt();
}
But even if I see from the logs that the request is aborted and the connection manager is shut down, the execution of the request is not interrupted/aborted and no exception is raised at the timeout set.
The request ends always after 20s.
Any idea why?
What you are probably seeing is that the DNS lookup is timing out. When your HTTP client attempts to make a connection, the first thing it does is try to resolve the hostname of your URL into an IP address. When doing this it doesn't take your timeout settings into account (those timeout values are only used when trying to actually make the socket connection). If you haven't got good Internet connectivity, your DNS lookup will just stall until it times out. Once that happens, your HTTP request should fail immediately with UnknownHostException.
Unfortunately you have no control over the DNS timeout, so the only way to solve your problem is to first determine if your DNS resolution is working. You should do this in a separate thread, and if you don't get successful host resolution within a few seconds, you know that your Internet connection is not reliable and you don't even have to attempt your HTTP request.
So you can try something like this:
HttpClient client = new DefaultHttpClient();
HttpParams httpParameters = client.getParams();
HttpRequestBase request = ...
request.setParams(httpParameters);
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutReal);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutReal);
request.setParams(httpParameters);
((DefaultHttpClient) client).setParams(httpParameters);
// The thread that is waiting to execute the HTTP request
final Thread waitingThread = Thread.currentThread();
Thread t = new Thread() {
boolean running = true;
public void run() {
try {
// Try to resolve the host name
InetAddress addr = InetAddress.getByName (hostname);
// Successful resolution, notify the waiting thread
if (running) {
// Signal the waiting thread that it can do the HTTP request now
waitingThread.interrupt();
}
} catch (Exception e) {
// Some problem, just ignore it
}
}
};
try {
// Start name resolution
t.start();
// Sleep for as long as we are willing to wait for the DNS resolution
Thread.sleep(4000);
// If we slept the entire time without getting interrupted, the DNS resolution took too long
// so assume we have no connectivity.
t.running = false; // We don't want to be interrupted anymore
// Don't even bother trying the HTTP request now, we've used up all the time we have
} catch (InterruptedException ie) {
// We got interrupted, so the DNS resolution must have been successful. Do the HTTP request now
HttpResponse httpResponse = client.execute(request);
}
I'm writing this code without trying it, so please forgive any typos or missing semicolons. You should get the idea.
You could try to use AndroidHttpClient instead of DefaultHttpClient. It has some specific settings for Android.
Alternatively, you could try to replace the following line:
HttpParams httpParameters = client.getParams();
with the following line
HttpParams httpParameters = new BasicHttpParams();
I don't know if this is the correct answer but, hope this helps.
The 20 seconds part of your question rfc2616 specifies 5 redirects times 4000 milis = 20 Seconds. The solution is to check for network connection before attempting network access.
// check for network connection
ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connMgr == null) {
return false; // I get a null here when there is no connection on my lg f6.
}
// check ok to process
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo == null || !networkInfo.isConnected()) {
return false;
}
return true;
Well, I had a similar problem. This is what I did to solve it.
First, I create an instance of the HttpParams:
HttpParams params = new BasicHttpParams();
Then, I don't set the settings manually. Instead I use the HttpConnectionParams class to do so. For example:
HttpConnectionParams.setStaleCheckingEnabled(params, false);
HttpConnectionParams.setConnectionTimeout(params, httpTimeout * 1000);
HttpConnectionParams.setSoTimeout(params, httpTimeout * 1000);
Then, I pass the created params when I instantiate the html client:
HttpClient client = new DefaultHttpClient(params);
I also use a Client Connection Manager, so, I pass it as the first parameter in the above call.
So, the trick is to create the HttpClient by passing already setted parameters and not chaging them afterwards.
Hope this works for you.
EDIT: Did you tried to block the redirects? HttpClientParams.setRedirecting(params, setRedirecting);
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'm using a PIC as web server it receives a form post to activate and deactivate a relay that is connected to a light bulb. This way I can switch it on and off from a web browser. I'm trying to make a widget in my android in order to control the switch on and off it just needs to send a form post with http basic authentication.
Which would it be the easiest way to achieve this? Since I don't have any android programming skills, I'm been looking in several online sites to build simple android apps but there is none which fill my requirements.
This is what I've used for simple html posting:
String baseurl = "http://yoursite.com" + yourpostdata;
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.
////httpClient.setParams(httpParameters); to edit
int timeoutSocket = 8000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpClient vClient = new DefaultHttpClient(httpParameters);
HttpGet vGet = new HttpGet(baseurl);
String response = "";
try {
ResponseHandler<String> vHandler = new BasicResponseHandler();
response = vClient.execute(vGet, vHandler);
}
catch (Exception e)
{
e.printStackTrace();
}
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 am currently faced to a strange problem.
I have to contact a web service, with a very long URL (there is some XML inside). The length of one of them is 943 characters.
Most of the time, the request failed with a NoHttpResponseException.
I newly added a RetryHandler, which do his job, and the request finally worked, but the execute time was 246 seconds!
I reduced the timeout, to something like 30 seconds, and occasionally, the request work.
Is there something to know about long URL to make it work better?
Or, is it just prohibited on Android?
I precise that all connection with another tinier URL (even like 200 chars) perfectly work.
Here the source code of the Http connection:
DefaultHttpClient hc = new DefaultHttpClient();
HttpProtocolParams.setUseExpectContinue(hc.getParams(), false);
HttpParams httpParameters = hc.getParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 5000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 10000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
hc.setParams(httpParameters);
HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest(IOException exception, int executionCount,
HttpContext context) {
// retry a max of x times
if(executionCount >= 5){
return false;
}
if(exception instanceof NoHttpResponseException){
return true;
} else if (exception instanceof ClientProtocolException){
return true;
}
return false;
}
};
hc.setHttpRequestRetryHandler(retryHandler);
url = Tool.prepareURL(url);
Log.d(LogFilter.EXECUTE, url);
HttpGet get = new HttpGet(url);
if (eTag != null) {
get.addHeader(HEADER_IF_NONE_MATCH, eTag);
}
long time = System.currentTimeMillis();
HttpResponse rp = hc.execute(get);
Log.d(LogFilter.EXECUTE, "temps execute: "+(System.currentTimeMillis()-time));
return rp;
Thank you for your time.
I think this is a server side problem and may not respond (much) to setting the timeouts. Have you tried pasting the long url into a browser?