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; } }
0 komentar:
Post a Comment