0

application crashes when it try to create a http connection when the phone is connected with Limited WIFI connection, can anyone tell me how to check the internet is working or not in WIFI. sorry for my poor english.

 FATAL EXCEPTION: main
        Process: com.bytemachine.aditya.vehicletracker, PID: 3626
        java.lang.NullPointerException: Attempt to invoke interface method 'org.apache.http.HttpEntity org.apache.http.HttpResponse.getEntity()' on a null object reference
        at com.bytemachine.aditya.vehicletracker.SearchScreen$SetConnectionForVehicleSearch.onPostExecute(SearchScreen.java:561)
        at com.bytemachine.aditya.vehicletracker.SearchScreen$SetConnectionForVehicleSearch.onPostExecute(SearchScreen.java:504)
        at android.os.AsyncTask.finish(AsyncTask.java:636)
        at android.os.AsyncTask.access$500(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:6946)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
  • 2
    the null pointe says it all? you may have not initiated the object propely. posting some code will help. – Razgriz May 28 '16 at 12:36
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Harshad Pansuriya May 28 '16 at 12:45

1 Answers1

1

To check network state, you can use the below code snippet :

    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();     
    if ((networkInfo != null && networkInfo.isConnectedOrConnecting()))
    {
     //network is connected, carry on your operations
    }

You can modify this code for checking for Wifi state as well using ConnectivityManager.TYPE_WIFI flag. For further details refer, https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html#DetermineConnection.

ruchita
  • 163
  • 2
  • 11