I am working on online app. [Problem] when internet is down or not available it gives me error [Force close], I tried to handle using broadCast Receiver but not meet exact solution, looking for better solution.
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
NetworkInfo info = intent
.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (null != info) {
String state = info.getState().toString();
if (state.equalsIgnoreCase("CONNECTED")) {
SplashScreen.isWiFiConnected = true;
Log.i("isWifiConnected", "=================TRUE");
} else {
SplashScreen.isWiFiConnected = false;
Log.i("isWifiConnected", "=================FALSE");
}
}
}
}
thanks.
static String data = null;
private static HttpPost httppost;
private static HttpParams httpParameters;
private static int timeoutConnection = 30000;
private static HttpClient httpclient = null;
private static HttpResponse response = null;
private static int responseCode=0;
public static ConnectivityManager mConnectivityManager;
public static NetworkInfo mNetworkInfo;
public static boolean isNetError=false;
/** Post Http data and returns final string and status on network */
public static void postHttp(String Url, Activity mActivity) {
try {
isNetError=false;
mConnectivityManager= (ConnectivityManager) mActivity.getSystemService(Context.CONNECTIVITY_SERVICE);
mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null && mNetworkInfo.isConnectedOrConnecting())
{
httppost = new HttpPost(Url);
httpParameters = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(httpParameters, timeoutConnection);
httpclient = new DefaultHttpClient(httpParameters);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("text", "some Text"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
// Execute HTTP Post Request
response = httpclient.execute(httppost);
data = EntityUtils.toString(response.getEntity());
}
else
isNetError=true;
} catch (Exception e) {
e.printStackTrace();
isNetError=true;
}
if (responseCode == 200)
{
isNetError=false;
System.out.println("final..." + data);
}
else
isNetError=true;
}
call this method in your doInBackground() of asyntask, and onPostExecute() check isNetError value and as mentioned in other answer of adding permission <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
if(isNetError)
//No internet
else
//do your stuff
Just use this function to check whether the Internet connection is available or not:
/**
* Checks if the device has Internet connection.
*
* @return <code>true</code> if the phone is connected to the Internet.
*/
public static boolean checkNetworkConnection(Context context)
{
final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi =connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile =connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(wifi.isAvailable()||mobile.isAvailable())
return true;
else
return false;
}
Don't forget to add permission inside the AndroidManifest.xml file: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With