应用程序在尝试创建http连接时崩溃,任何人都可以告诉我如何在WIFI中检查互联网是否正常工作。抱歉我的英语很差。
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)
答案 0 :(得分:0)
要检查网络状态,您可以使用以下代码段:
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if ((networkInfo != null && networkInfo.isConnectedOrConnecting()))
{
//network is connected, carry on your operations
}
您可以使用ConnectivityManager.TYPE_WIFI标志修改此代码以检查Wifi状态。有关详细信息,请参阅https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html#DetermineConnection。