About Francesco Azzola

I'm a senior software engineer with more than 15 yrs old experience in JEE architecture. I'm SCEA certified (Sun Certified Enterprise Architect), SCWCD, SCJP. I'm an android enthusiast and i've worked for long time in the mobile development field.

Android HTTP Client : Network error and ConnectivityManager

In this post I will show how to handle connection errors or the situation when the connection changes (i.e from UMTS to WI-FI). In all these cases we have to gracefully handle these errors and implement some strategies to try to reconnect if possible.

In a perfect world when we connect to a remote server with HTTP protocol everything works perfectly, we don’t have to worry about connection errors, server under heavy request load or unstable network. In some of last posts we talked about how we can connect to a remote server and how we can send and receive data. In the real world, usually while we are connected to a remote server we walk or run or even we are in car, so that the network signal can vary or it can suddenly break. At the same time Android can device to switch from UMTS to WI-FI or vice versa or it can switch from one APN to another and so on. When we code our app, we have to take into account all these situation and handle them correctly. We can simplify and try to categorize all these events in two classes:

  • Network error
  • Network configuration changes

If we used Apache HTTP Client there’s already a mechanism that helps us, we could implement HttpRequestRetryHandler interface with our retry mechanism logic or a default implementation. If we use the standard Android HTTP connection we have to implement is by ourselves.

Handling connection error

In this post I showed how to connect to a remote server and we created an HttpClient class that manages the connection. In this case we extend this class handling possible network error. To do it we use the call back mechanism, in other word we set up a listener that gets notified when there is a connection error. To do it we create an interface:

public interface HttpConnectionRetryHandler {

    public boolean shouldRetry(Throwable t, int attemptNumber) ;
}

This interface has just only one method where we implement our logic to handle or not the network error. We pass the exception and a counter that holds the number of attempt to re-establish the connection. The HttpClient then has to be modified to handle the connection error. It doesn’t have to raise an exception as soon as it can’t connect to the server but has to ask to our class that implements HttpConnectionRetryHandler if it should raise the exception or try to connect again.

public void connect(String method, Properties props) throws HttpClientException {
    boolean status  = true;
    int attemptNumber = 0;
    while (status) {
        try {
            attemptNumber++;
            doConnection(method, props);
            status = false;
        }
        catch(Throwable t) {
            if (handler != null) {
                status = handler.shouldRetry(t, attemptNumber);
                if (!status)
                    throw new HttpClientException(t);
            }
            else {                    
                throw new HttpClientException(t);
            }
        }
    }

}

At line 12 when we have an exception we call handler.shouldRetry to verify if our connection error handler wants to handle the error or not. If not we raise the exception otherwise we keep on trying to reconnect. In our class that handles the error we implement our business logic. The simplest thing is checking the number of attempt. In this case we have:

public class DefaultHttpConnectionRetryHandler implements HttpConnectionRetryHandler {

    @Override
    public boolean shouldRetry(Throwable t, int attemptNumber) {
        System.out.println("Attempt ["+attemptNumber+"]");
        if (attemptNumber > 5)
            return false;

        return true;
    }

}

So when we call the HttpClient we have:

...
client.setHandler(new DefaultHttpConnectionRetryHandler());
...

Handling network configuration changes

As we said early while we move with our smartphone or tablet the network configuration can chance switching from WI-FI to UMTS or vice versa or from UTMS to switching from one APN to another (maybe in roaming). We have to handle all these changes and try to use the network configurations to re-establish the connection. To handle these events we can use a Broadcast receiver that gets notified when a network configuration change occurs. Changes in APN configuration or in changes in connectivity is handle by ConnectivityManager that broadcast some information to its subscribers. So the first thing we code our broadcast receiver:

public class ConnectionMonitorReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context ctx, Intent intent) {
        System.out.println("Receive..");
        NetworkInfo netInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

        if (netInfo == null)
            return ;

        System.out.println("Here");
        if (netInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
            String proxyHost = System.getProperty( "http.proxyHost" );

            String proxyPort = System.getProperty( "http.proxyPort" );

            if (proxyHost != null)
                HttpClient.getInstance().setProxy(proxyHost, proxyPort);
            else
                HttpClient.getInstance().setProxy(null, null);

        }
    }
}

At line 1 we simply extends the BroadcastReceiver and we implement onReceive method (line 4). At line 6 we get the information shipped inside the intent broad and implements our logic. In our case we simply check if the proxy is changed but we could implements other type of business logic.

The last thing we have to do is register our broadcast receiver so that it can be called. We do it in our onCreate method of the Activity:

registerReceiver(new ConnectionMonitorReceiver(), new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

Running the code we have:

Immagine1 Immagine2
 

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

2 Responses to "Android HTTP Client : Network error and ConnectivityManager"

  1. Rzenks mj says:

    Can I use this concept to connect my app with some other external wifi devices.?

    im planning to do a home automation project ,

    when i click button on android pp, it should activate the targeted device ,

    Can u please help me how can i implement
    onClickListener, onClick methods using a button ?

  2. enrique says:

    Hello Francesco, can you post the source code of the app that you made for this example, it would very nice, thx and great post :)

    Greetings from Chile :)

Leave a Reply


− 1 = eight



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close