From e107c014f07d64e9fee6c0bb7c6940d0cfe11f77 Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Sun, 23 Sep 2018 20:50:39 -0500 Subject: [PATCH] The network service will retry a connection after 5 seconds in case of socket failure --- .../graphenej/api/android/NetworkService.java | 14 +++++++++++++- .../cy/agorise/labs/sample/SampleApplication.java | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java b/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java index f8b8c65..73c501f 100644 --- a/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java +++ b/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java @@ -3,7 +3,9 @@ package cy.agorise.graphenej.api.android; import android.app.Service; import android.content.Intent; import android.os.Binder; +import android.os.Handler; import android.os.IBinder; +import android.os.Looper; import android.util.Log; import com.google.gson.Gson; @@ -75,6 +77,9 @@ public class NetworkService extends Service { public static final int NORMAL_CLOSURE_STATUS = 1000; + // Time to wait before retrying a connection attempt + private final int DEFAULT_RETRY_DELAY = 5000; + public static final String KEY_USERNAME = "key_username"; public static final String KEY_PASSWORD = "key_password"; @@ -572,7 +577,14 @@ public class NetworkService extends Service { Log.e(TAG,"Giving up on connections"); stopSelf(); }else{ - connect(); + Handler handler = new Handler(Looper.getMainLooper()); + handler.postDelayed(new Runnable() { + @Override + public void run() { + Log.d(TAG,"Retrying"); + connect(); + } + }, DEFAULT_RETRY_DELAY); } } }; diff --git a/sample/src/main/java/cy/agorise/labs/sample/SampleApplication.java b/sample/src/main/java/cy/agorise/labs/sample/SampleApplication.java index 1e246e4..1e2a95e 100644 --- a/sample/src/main/java/cy/agorise/labs/sample/SampleApplication.java +++ b/sample/src/main/java/cy/agorise/labs/sample/SampleApplication.java @@ -19,7 +19,7 @@ public class SampleApplication extends Application { int requestedApis = ApiAccess.API_DATABASE | ApiAccess.API_HISTORY | ApiAccess.API_NETWORK_BROADCAST; NetworkServiceManager networkManager = new NetworkServiceManager.Builder() - .setUserName("nelson") + .setUserName("username") .setPassword("secret") .setRequestedApis(requestedApis) .setCustomNodeUrls("wss://eu.nodes.bitshares.ws")