0
String url = "http://xyzcloud.com/xyz/xyz.php";

this the url am trying to get the data from cloud,if internet is connected then async task will execute i wrote this method for checking wether internet is connected or not, here in my mobile wifi is connetecd when am trying to execute it is giving exception

public boolean checkOnlineState()
    {
        boolean val = false;
        ConnectivityManager CManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo NInfo = CManager.getActiveNetworkInfo();
        if (NInfo != null && NInfo.isConnectedOrConnecting()) {
            try {
                if (InetAddress.getByName(url).isReachable(10000))
                {
                    // host reachable
                    val=true;
                    Log.e("checkOnlineState", "" + val);
                }
                else
                {
                    // host not reachable
                    val = false;
                    Log.e("checkOnlineState", "" + val);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return val;
    }
veeraprasad
  • 61
  • 1
  • 6
  • this is the exception am getting FATAL EXCEPTION: main Process: com.xyz.xyz, PID: 13405 android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1288) at java.net.InetAddress.lookupHostByName(InetAddress.java:432) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:253) at java.net.InetAddress.getByName(InetAddress.java:306) – veeraprasad Dec 30 '16 at 07:54
  • handle socket timeout and connection time out in the catch block – Raghunandan Dec 30 '16 at 07:57
  • after adding two exceptions it returns false – veeraprasad Dec 30 '16 at 08:08
  • http://stackoverflow.com/questions/16439587/android-os-networkonmainthreadexception-with-android-4-2 – IntelliJ Amiya Dec 30 '16 at 08:09
  • how about putting val = false; to catch block ? – Chris Maverick Dec 30 '16 at 10:19

1 Answers1

0
import android.app.Service;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionDetector {

    private Context context;

    public ConnectionDetector(Context context) {

        this.context = context;
    }

    public boolean isConnected() {

        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Service.CONNECTIVITY_SERVICE);

        if ( connectivity != null) {

            NetworkInfo info = connectivity.getActiveNetworkInfo();

            if (info != null) {

                if (info.getState() == NetworkInfo.State.CONNECTED) {

                    return true;
                }
            }
        }

        return false;
    }
}

Then for example, you can make use of it:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ConnectionDetector cd = new ConnectionDetector(this);

        if (cd.isConnected()) {
            // do something
        } else {
            //do something
        }

        setContentView(R.layout.activity_main);
    }
}

I use this approach in my apps (which requires Internet connection) and didn't face any problem yet.

EtherPaul
  • 424
  • 10
  • 19
  • wife is connected but no INTERNET access in this case it is giving this exception org.apache.http.conn.HttpHostConnectException: Connection to http://xyzcloud.com refused – veeraprasad Dec 30 '16 at 08:29
  • Have you added permision to your Manifest file: – EtherPaul Dec 30 '16 at 08:37