Wednesday, 27 May 2015

How to check server connection is available or not in android

Add the below function to your Activity Class to check server connection

public boolean isConnectedToServer(String url, int timeout) {
    URL myUrl = new URL(url);
    URLConnection connection myUrl .openConnection();
    connection .setConnectTimeout(timeout);
    connection .connect();
    return true;
} catch (Exception e) {
    // Handle your exceptions
    return false;

and also add intent permission in your manifest file

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission  android:name="android.permission.ACCESS_NETWORK_STATE" />

Happy Coding:)