开发者

Query regarding app using Internet connection

I have an application that uses Internet connection but when there is no internet connection available the application stop responding. Can anyone help on how i can resolve this problem. What I have 开发者_开发百科thought that i can display an error when there no internet connection available but I dont know the way to do it.


Required permission is :

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

Check Internet connection for each request

public boolean isOnline() {
        NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }

Use above method as

if(isOnline()) {
   //Write code of request
} else {
   showDialog("Internet connection error", "Connection is not available.")
}

Show dialog method

private void showDialog(String title, String text) {
        // Access denied. Show dialogue to user
        AlertDialog.Builder alertbox = new AlertDialog.Builder(YOUR_ACTIVITY.this);
        alertbox.setTitle(title);
        alertbox.setIcon(android.R.drawable.ic_dialog_info);
        alertbox.setMessage(text);
        alertbox.setNeutralButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                //finish(); //DO nothing
            }
        });
        alertbox.show();
    }

Edited : Replace YOUR_ACTIVITY.this with activity name in which your are using this code.


You can show an AlertDialog to show the user that there is no internet connection.And if your application can't go on without connection,after clickin cancel button in AlertDialog, you can finish your activity and close the application.


Check out my HTTPclass.java class, how I did that, I have a method connect(url) that takes a String of url and connects it, if connection is received it responds and gives the responseCode = 200 or HTTP.OK, and if the connection is not connected I handle a catch block and set the responseCode = -1 which means its not connected.

So, you can do something like that only. When responsecode is -1 you can show the Message that "Internet Not Connected".

public class HTTPClass
{
    private static int responseCode = -1;
    private static HttpURLConnection httpconn = null;        
    private static URLConnection conn = null;
    private static URL urlobj;

    public static int connect(String url) throws IOException
    {
        try
        {
            urlobj = new URL(url);
            conn = urlobj.openConnection();
            httpconn= (HttpURLConnection)conn;
            httpconn.setConnectTimeout(5000);
            httpconn.setDoInput(true);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        try
        {
            responseCode = httpconn.getResponseCode();
        }
        catch(Exception e)
        {
            responseCode = -1;
            e.printStackTrace();
        }
        return responseCode;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜