From 646ca355d01b6efa6fd1d73cf6b7e7d3555cbe6d Mon Sep 17 00:00:00 2001
From: Volker Richert <v.richert@addmore.de>
Date: Wed, 30 Nov 2016 21:05:59 +0100
Subject: [PATCH] fix not resending to NS if upload fails THX @pogman

---
 .../NightscoutUploadIntentService.java        | 25 +++++++++++--------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/app/src/main/java/info/nightscout/android/upload/nightscout/NightscoutUploadIntentService.java b/app/src/main/java/info/nightscout/android/upload/nightscout/NightscoutUploadIntentService.java
index ff4bb71..b542315 100644
--- a/app/src/main/java/info/nightscout/android/upload/nightscout/NightscoutUploadIntentService.java
+++ b/app/src/main/java/info/nightscout/android/upload/nightscout/NightscoutUploadIntentService.java
@@ -10,6 +10,8 @@ import android.preference.PreferenceManager;
 import android.support.v4.content.LocalBroadcastManager;
 import android.util.Log;
 
+import org.apache.http.HttpResponse;
+import org.apache.http.StatusLine;
 import org.apache.http.client.ResponseHandler;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.StringEntity;
@@ -169,22 +171,22 @@ public class NightscoutUploadIntentService extends IntentService {
                 addMbgEntry(entriesBody, record);
             }
 
-            uploadToNightscout(new URL(baseURL + "/entries"), secret, entriesBody);
+            boolean isUploaded = uploadToNightscout(new URL(baseURL + "/entries"), secret, entriesBody);
 
-            for(int i = 0; i < devicestatusBody.length(); i++) {
-                uploadToNightscout(new URL(baseURL + "/devicestatus"), secret, devicestatusBody.getJSONObject(i));
+            for(int i = 0; isUploaded && i < devicestatusBody.length(); i++) {
+                isUploaded &= uploadToNightscout(new URL(baseURL + "/devicestatus"), secret, devicestatusBody.getJSONObject(i));
             }
 
-            // Yay! We uploaded. Tell Realm
-            // FIXME - check the upload succeeded!
-            mRealm.beginTransaction();
-
-            for (PumpStatusEvent updateRecord : records) {
-                updateRecord.setUploaded(true);
+            if (isUploaded) {
+                // Yay! We uploaded. Tell Realm
+                // FIXME - check the upload succeeded!
+                mRealm.beginTransaction();
+                for (PumpStatusEvent updateRecord : records) {
+                    updateRecord.setUploaded(true);
+                }
+                mRealm.commitTransaction();
             }
 
-            mRealm.commitTransaction();
-
         } catch (Exception e) {
             Log.e(TAG, "Unable to post data", e);
         }
@@ -236,6 +238,7 @@ public class NightscoutUploadIntentService extends IntentService {
             httpclient.execute(post, responseHandler);
         } catch (Exception e) {
             Log.w(TAG, "Unable to post data to: '" + post.getURI().toString() + "'", e);
+            return false;
         }
 
         return true;
-- 
GitLab