Testing connection to server on Android
<p>I want to test the connection to my localhost ( or any URL ) , i made a simple code to test, i add the needed permission to manifest , but the fonction still returning "false".</p>

<p>here's my code : </p>

<pre><code>import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.net.URL;
import java.net.URLConnection;

public class MainActivity extends AppCompatActivity {
    String adrs = "http://wwww.google.com";
    int time = 10000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        isConnectedToServer(adrs,time);



        Button button = (Button) findViewById(R.id.btn);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

          if (isConnectedToServer(adrs,time) == true) { Toast.makeText(getBaseContext(),"Connecté !",Toast.LENGTH_LONG).show();}
                else {Toast.makeText(getBaseContext(),"Connexion perdue",Toast.LENGTH_LONG).show();}



                isConnectedToServer(adrs,time);


            }
        });
    }

    public boolean isConnectedToServer(String url, int timeout) {
        try{
            URL myUrl = new URL(url);
            URLConnection connection = myUrl.openConnection();
            connection.setConnectTimeout(timeout);
            connection.connect();
            Toast.makeText(getBaseContext(),"Connecté !",Toast.LENGTH_LONG).show();
            return true;
        } catch (Exception e) {
            // Handle your exceptions
            Toast.makeText(getBaseContext(),"Connexion perdue",Toast.LENGTH_LONG).show();
            return false;
        }
    }
}
</code></pre>

<p>and this is my Manifest </p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.fisher.testconnection"&gt;
    &lt;uses-permission android:name="android.permission.INTERNET"/&gt;

    &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/&gt;

    &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&gt;

    &lt;uses-permission android:name="android.permission.CALL_PHONE" /&gt;

    &lt;application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"&gt;
        &lt;activity android:name=".MainActivity"&gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;

                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;

&lt;/manifest&gt;
</code></pre>