amodkanthe
amodkanthe

Reputation: 4530

android.net.ConnectivityManager$TooManyRequestsException

Note: Don't mark this as duplicate as this existing answer does not solved my query android.net.ConnectivityManager$TooManyRequestsException in android while network call

Update Wed 28 Jun : Found this is happening only on Android 11 devices.

I have added a callback to notify device is connected to internet or not code is giving me following exception in firebase I have never observed this issue on the app following is the exception log

Exception android.net.ConnectivityManager$TooManyRequestsException:
at android.net.ConnectivityManager.convertServiceException 
(ConnectivityManager.java:3698)
at android.net.ConnectivityManager.sendRequestForNetwork 
(ConnectivityManager.java:3886)
at android.net.ConnectivityManager.registerDefaultNetworkCallback 
(ConnectivityManager.java:4378)
at android.net.ConnectivityManager.registerDefaultNetworkCallback 
(ConnectivityManager.java:4345)
at com.myapp.android.core.network.PieConnectionStatusWatcher.registerCallback 
(SourceFile:38)
at com.myapp.android.core.network.NetworkStateMonitor.addCallback (SourceFile:62)
at com.myapp.android.core.mvvm.repository.MyDataRepository.notifyIfAppIsOnline 
(SourceFile:161)
at com.myapp.android.banner.viewmodel.MyViewModel.startReloadIfAppIsOnlineAgain 
(SourceFile:287)
 at com.myapp.android.viewmodel.MyViewModel.onAdLoadingFailed (SourceFile:262)
 at com.myapp.android.core.mvvm.repository.MyDataRepository.lambda$loadAd$2
 at android.os.Handler.handleCallback (Handler.java:938)
 at android.os.Handler.dispatchMessage (Handler.java:99)
 at android.os.Looper.loop (Looper.java:257)
 at android.app.ActivityThread.main (ActivityThread.java:8390)
 at java.lang.reflect.Method.invoke
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run 
 (RuntimeInit.java:603)
 at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:947)

I made sure to add and remove callback as mentioned in few solutions as below

connectivityManager = requireContext().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
connectivityManager?.registerDefaultNetworkCallback(connectionCallback)

Unregister:

connectivityManager?.unregisterNetworkCallback(connectionCallback)

I am still able to see this crash any reason how to fix this?

Condition under which I add network callback when api call fails due to network not available and unregister call either when user comes back online or activity is destroyed.

 public Object notifyIfAppIsOnline(Runnable onAppIsOnline) {
    NetworkStateMonitor.Callback connectionStateListener = new NetworkStateMonitor.Callback() {
        @Override
        public void onNetworkStateChanged(boolean isOnline) {
            if (isOnline) {
                networkStateMonitor.removeCallback(this);
                onAppIsOnline.run();
            }
        }
    };

    networkStateMonitor.addCallback(connectionStateListener);

    return connectionStateListener;
}

public void onDestroy() {
    if (appIsOnlineNotification != null) {
        myRepository.cancelAppOnlineNotification(appIsOnlineNotification);
    }
 }

Upvotes: 5

Views: 3675

Answers (0)

Related Questions