-6

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.

How can i check this wifi connection share the internet.

Here is my class which has two method for internet connection checking and network connection checking.

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 && networkInfo.isConnected()) {
            return true;
        }
        return false;
    }
}

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

ConnectionDetector cd=new ConnectionDetector(getContext());

        if (cd.isConnectingToInternet()) {
            new TabJson().execute();
        }
        else{
            dialog_popup();
        } 

Here, is an exception

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.<init>(HttpConnection.java:70)
        at libcore.net.http.HttpConnection.<init>(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)

how can i solve this problem??? Please help me to solve out this problem.

Milan Gajera
  • 962
  • 2
  • 14
  • 41

2 Answers2

0

it is because you are calling httprequest on main thread . use AsyncTask instead .

KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
0

This Exception is thrown because you performing networking operation in your main Thread. Do it in your Background Thread. ie AsyncTask

class Task extends AsyncTask<String, void, boolean> {

private Exception exception;

protected boolean doInBackground(String... xyz) {
    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 && networkInfo.isConnected()) {
        return true;
    }
    return false;
}


}
Sumanth Jois
  • 3,146
  • 4
  • 27
  • 42