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 - Url ConnectThis the basis for web service. Android + Web Service is a future will be for me. With open platform you can access open information. That's really great I think. Simple, with a piece of code to access web service in Android,… Read More
  • Android Programming - isServerAccessibleThis 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. . :) pu… Read More
  • 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

0 komentar:

Copyright © 2025 Johannes Dwi Cahyo | Powered by Blogger

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