How can i check my android device having a internet connection in android? : <p>Explanation:
           I am trying to connect my device which has internet connection.I added a permission in manifiest both network state and internet.I check correctly my device is connected with specific network.e.g. WIFI.</p>

<p>How can i check this wifi connection share the internet.</p>

<p>Here is my class which has two method for internet connection checking and network connection checking.</p>

<pre><code>package comman;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import java.net.URL;
import java.io.IOException;
import java.net.HttpURLConnection;


public class ConnectionDetector {
    private Context _context;

    public ConnectionDetector(Context context) {
        this._context = context;
    }

    public boolean isConnectingToInternet() {
        if (networkConnectivity()) {
            try {
                HttpURLConnection urlc = (HttpURLConnection) (new URL(
                        "http://www.google.com").openConnection());
                urlc.setRequestProperty("User-Agent", "Test");
                urlc.setRequestProperty("Connection", "close");
                urlc.setConnectTimeout(3000);
                urlc.setReadTimeout(4000);
                urlc.connect();
                // networkcode2 = urlc.getResponseCode();
                return (urlc.getResponseCode() == 200);
            } catch (IOException e) {
                return (false);
            }
        } else
            return false;
    }

    private boolean networkConnectivity() {
        ConnectivityManager cm = (ConnectivityManager) _context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null &amp;&amp; networkInfo.isConnected()) {
            return true;
        }
        return false;
    }
}
</code></pre>

<p>Here is the code where i am trying to check whether device having internet connection or not?</p>

<pre><code>ConnectionDetector cd=new ConnectionDetector(getContext());

        if (cd.isConnectingToInternet()) {
            new TabJson().execute();
        }
        else{
            dialog_popup();
        } 
</code></pre>

<p>Here, is an exception </p>

<pre><code>FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.angelnx.cricvilla.cricvilla/com.angelnx.cricvilla.cricvilla.MainActivity}: android.os.NetworkOnMainThreadException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
        at android.app.ActivityThread.access$700(ActivityThread.java:143)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4950)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
        at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
        at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
        at java.net.InetAddress.getAllByName(InetAddress.java:214)
        at libcore.net.http.HttpConnection.&lt;init&gt;(HttpConnection.java:70)
        at libcore.net.http.HttpConnection.&lt;init&gt;(HttpConnection.java:50)
        at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
        at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
        at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
        at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
        at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
        at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
        at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
        at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
        at comman.ConnectionDetector.isConnectingToInternet(ConnectionDetector.java:29)
        at com.angelnx.cricvilla.cricvilla.HomeFragment.onCreateView(HomeFragment.java:130)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
        at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
        at android.app.Activity.performStart(Activity.java:5187)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2083)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135) 
        at android.app.ActivityThread.access$700(ActivityThread.java:143) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:137) 
        at android.app.ActivityThread.main(ActivityThread.java:4950) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:511) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 
        at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>how can i solve this problem???
Please help me to solve out this problem.</p>
