Better JSONObject logging, fixed bug with pushed_at values when 0

pull/14/head
Sam Bosley 13 years ago
parent f42d3a2b5f
commit e8b25e8fdd

@ -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<String> 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) {/**/}
}
}
}
}

@ -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);
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);
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,6 +186,7 @@ public class ActFmInvoker {
ActFmServiceException {
try {
String request = createFetchUrl("api2/" + API_VERSION, "synchronize");
if (Constants.DEBUG)
Log.e("act-fm-post", request);
List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
pairs.add(new BasicNameValuePair("token", token));
@ -180,8 +194,11 @@ public class ActFmInvoker {
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;

@ -67,7 +67,8 @@ public abstract class ClientToServerMessage<TYPE extends RemoteModel> {
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;

Loading…
Cancel
Save