Sunday, September 11, 2011

Android Programming - isServerAccessible

This is my snippet to check wheter web service server accessible or not. Don't forget to edit your Manifest code..
First Code check internet connection available or not, the second check the server accessible or not. . :)
 public boolean isNetworkAvailable() {
        Log.i("isNetworkAvailable", "cek network connection");
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }
public static boolean isServerAccessable(){
        Log.i("isServerAccesable", "check server connection");
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet("http://10.0.2.2/jsonserv/");
            try {
                HttpResponse response = client.execute(request);
                // Check if server response is valid
                StatusLine status = response.getStatusLine();
                if (status.getStatusCode() == HTTP_STATUS_OK) {
                    return true;
                } else {
                    return false;
                }
            } catch (Exception e) {
                return false;
            }
        }

Categories: , , ,

Related Posts:

  • Android Programming - Access LocalhostWhen I developed an application that connect to my own web service ( I made my own simple web service too ^-^), I got this error "java.net.ConnectException localhost/127.0.0.1:8080 - Connection Refused". Hei, why my applicati… Read More
  • Android Programming - Hello WorldOkay, the preparation phase is complete. You can now make Android Project. Let us start with the classic way in which a program Hello World. Let us immediately make, you need not know anything yet, we make first and I will… Read More
  • Android Programming - Fast Way Preserving Sqlite DbActually this is some snippet I found from browsing, the guy who must be credited to this code is Omar Flores, sorry I can't found your link pal :). It helping me to copy already made SQLite database to my application, it mak… Read More
  • Android Push Notification !-- @page { margin: 0.79in } P { margin-bottom: 0.08in } -- Dalam OS Android sampai saat ini belum tersedia dukungan untuk push notification secara native seperti milik Blackberry ataupu iOS. Secara fasilitas UI… Read More
  • Android Programming - Free Ebook (3)1. Wrox - Android Application Development - Reto Meier A hands-on guide to building mobile applications, Professional Android Application Development features concise and compelling examples that show you how… Read More

0 komentar:

Copyright © 2025 Johannes Dwi Cahyo | Powered by Blogger

Design by Anders Noren | Blogger Theme by NewBloggerThemes.com | BTheme.net      Up ↑