Для метода get:
private void executeReq(URL urlObject) throws IOException{
HttpURLConnection conn = null;
conn = (HttpURLConnection) urlObject.openConnection();
conn.setReadTimeout(100000); //Milliseconds
conn.setConnectTimeout(150000); //Milliseconds
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Start connect
conn.connect();
String response = convertStreamToString(conn.getInputStream());
Log.d("Response:", response);
}
Вы можете вызвать его с помощью
try {
String parameters = ""; //
URL url = new URL("http://alefon.com" + parameters);
executeReq(url);
}
catch(Exception e){
//Error
}
Чтобы проверить подключение к Интернету, используйте:
private void checkInternetConnection() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (null == ni)
Toast.makeText(this, "no internet connection", Toast.LENGTH_LONG).show();
else {
Toast.makeText(this, "Internet Connect is detected .. check access to sire", Toast.LENGTH_LONG).show();
//Use the code above...
}
}