Crash When Is No Internet Connection
The following code crashes when is no interenet connection : public String gourl(String myurl,String params) { URL url; String rasp = ''; try {
Solution 1:
Check connection before execute your method, something like that:
publicbooleanisNetworkConnected() {
finalConnectivityManagerconMgr= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
finalNetworkInfoactiveNetwork= conMgr.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.getState() == NetworkInfo.State.CONNECTED;
}
Solution 2:
Have you checked what the values of myurl
and params
are in the method?
It may be that url.openStream()
is failing and causing a NullPointerException.
It's also helpful to rather do something like:
Log.w("DHA", "EXCEPTIE URL:" + e.toString());
Then you will see what the Exception is rather than having to guess.
Solution 3:
Check for all Internet connection
For Wifi
public boolean isWifi(Context context){
try{
WifiManager wifi=(WifiManager)
context.getSystemService(Context.WIFI_SERVICE);
if(wifi.isWifiEnabled()){
returntrue;
}else{
returnfalse;
}
}
catch(Exception e){
e.getMessage();
returnfalse;
}
}
For Other Network
public boolean isOline(Context context){
try{
ConnectivityManager cm=(ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm==null)
returnfalse;
NetworkInfo info=cm.getActiveNetworkInfo();
if(info==null)
returnfalse;
return info.isConnectedOrConnecting();
}
catch(Exception e){
e.getMessage();
returnfalse;
}
}
If any of them is present then process WS else show alert.And Never forget to mention
<uses-permissionandroid:name="android.permission.INTERNET" /><uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"/><uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>
Post a Comment for "Crash When Is No Internet Connection"