diff --git a/api/src/com/todoroo/andlib/utility/AndroidUtilities.java b/api/src/com/todoroo/andlib/utility/AndroidUtilities.java index 4c8cf331b..d2fa6a04a 100644 --- a/api/src/com/todoroo/andlib/utility/AndroidUtilities.java +++ b/api/src/com/todoroo/andlib/utility/AndroidUtilities.java @@ -23,10 +23,15 @@ import java.security.MessageDigest; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; +import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + import android.app.Activity; import android.content.BroadcastReceiver; import android.content.ContentValues; @@ -909,4 +914,37 @@ public class AndroidUtilities { return extension; } + /** + * Logs a JSONObject using in a readable way + */ + @SuppressWarnings("nls") + public static void logJSONObject(String tag, JSONObject object) { + if (object == null) { + Log.e(tag, "JSONOBject: null"); + return; + } else { + Log.e(tag, "Logging JSONObject"); + } + Iterator keys = object.keys(); + while (keys.hasNext()) { + String key = keys.next(); + JSONArray array = object.optJSONArray(key); + if (array != null) { + Log.e(tag, " " + key + ": Array"); + for (int i = 0; i < array.length(); i++) { + try { + Object elem = array.get(i); + Log.e(tag, " Index " + i + ": " + elem); + } catch (JSONException e) {/**/} + } + } else { + try { + Object value = object.get(key); + Log.e(tag, " " + key + ": " + value); + } catch (JSONException e) {/**/} + } + } + + } + } diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmInvoker.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmInvoker.java index d1193c18a..94ddcc0ff 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmInvoker.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmInvoker.java @@ -30,8 +30,10 @@ import com.timsu.astrid.R; import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.service.RestClient; +import com.todoroo.andlib.utility.AndroidUtilities; import com.todoroo.andlib.utility.Pair; import com.todoroo.andlib.utility.Preferences; +import com.todoroo.astrid.utility.Constants; @SuppressWarnings("nls") public class ActFmInvoker { @@ -126,10 +128,15 @@ public class ActFmInvoker { ActFmServiceException { try { String request = createFetchUrl(api, method, getParameters); - Log.e("act-fm-invoke", request); + + if (Constants.DEBUG) + Log.e("act-fm-invoke", request); + String response = restClient.get(request); - Log.e("act-fm-invoke-response", response); JSONObject object = new JSONObject(response); + + if (Constants.DEBUG) + AndroidUtilities.logJSONObject("act-fm-invoke-response", object); if(object.getString("status").equals("error")) throw new ActFmServiceException(object.getString("message"), object); return object; @@ -155,10 +162,16 @@ public class ActFmInvoker { ActFmServiceException { try { String request = createFetchUrl(null, method, getParameters); - Log.e("act-fm-post", request); + + if (Constants.DEBUG) + Log.e("act-fm-post", request); + String response = restClient.post(request, data); - Log.e("act-fm-post-response", response); JSONObject object = new JSONObject(response); + + if (Constants.DEBUG) + AndroidUtilities.logJSONObject("act-fm-post-response", object); + if(object.getString("status").equals("error")) throw new ActFmServiceException(object.getString("message"), object); return object; @@ -173,15 +186,19 @@ public class ActFmInvoker { ActFmServiceException { try { String request = createFetchUrl("api2/" + API_VERSION, "synchronize"); - Log.e("act-fm-post", request); + if (Constants.DEBUG) + Log.e("act-fm-post", request); List pairs = new ArrayList(); pairs.add(new BasicNameValuePair("token", token)); pairs.add(new BasicNameValuePair("data", data.toString())); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, HTTP.UTF_8); String response = restClient.post(request, entity); - Log.e("act-fm-post-response", response); JSONObject object = new JSONObject(response); + + if (Constants.DEBUG) + AndroidUtilities.logJSONObject("act-fm-post-response", object); + if(object.getString("status").equals("error")) throw new ActFmServiceException(object.getString("message"), object); return object; diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ClientToServerMessage.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ClientToServerMessage.java index d61b13da2..3c928d6c7 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ClientToServerMessage.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ClientToServerMessage.java @@ -67,7 +67,8 @@ public abstract class ClientToServerMessage { json.put(TYPE_KEY, getTypeString()); json.put(TABLE_KEY, table); json.put(UUID_KEY, uuid); - json.put(PUSHED_AT_KEY, DateUtilities.timeToIso8601(pushedAt, true)); + String dateValue = DateUtilities.timeToIso8601(pushedAt, true); + json.put(PUSHED_AT_KEY, dateValue != null ? dateValue : 0); serializeExtrasToJSON(json); } catch (JSONException e) { return null;