0

I'm am developing an Android app that uses Volley to send some data entered by the user to my server. I am also saving this data in a local SQLite database. All this works just fine. The problem occours if the internet connection is lost. How do I know when I must try to re-upload the data.

If the network connection is lost I can use a BroadcastReceiver to find out when the connection is established. However how do I ensure this works even when the device is connected to the network but there is no access to the internet.

Pratik
  • 1
  • 1

2 Answers2

0

Try this way

1.Check your Internet Connection before you want to upload data to some View (Ex.ListView)

If Available, upload server data.
else
upload local/saved data.

This may helps you

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
0

Create a broadcast receiver to listen to the internet changes and register it:

 class NetworkStateBroadcastReceiver extends BroadcastReceiver {
 public void onReceive(Context context, Intent intent) {
 ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
 NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
 if(networkInfo.isConnected()){
     //netwok just connected, upload
 }
}

Create an instance and register it.(may be in application class, which runs throughout your app)

 reciever = new NetworkStateBroadcastReceiver();
 registerReceiver(mReciever, new intentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
Mr.Rao
  • 321
  • 2
  • 6
  • Hi I tried out your solution. It works fine if the reason I could not connect to the server was because my network was disconnected. However if I am connected to the same network but the internet is then disconnected, how can I know when the internet connection is back. (I tried removing the Ethernet cable while connected to WiFi and then putting it back. I want to be notified when internet connectivity is resumed.) – Pratik Jun 30 '16 at 11:57
  • IntentFilter filter = new intentFilter(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); use this intenet filter and write another broadcast reciever. Refer this: http://stackoverflow.com/questions/9353005/android-wifi-how-to-detect-when-specific-wifi-connection-is-available – Mr.Rao Jul 04 '16 at 11:59