Improved JSON message serialization

pull/14/head
Sam Bosley 13 years ago
parent 0622e43ff9
commit 2fcad7c3e3

@ -18,7 +18,6 @@ import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -185,13 +184,12 @@ public class ActFmInvoker {
}
}
public JSONObject postSync(JSONArray data, MultipartEntity entity, boolean changesHappened, String tok) throws IOException,
public JSONObject postSync(String data, MultipartEntity entity, boolean changesHappened, String tok) throws IOException,
ActFmServiceException {
try {
String dataString = data.toString();
String timeString = DateUtilities.timeToIso8601(DateUtilities.now(), true);
Object[] params = { "token", tok, "data", dataString, "time", timeString };
Object[] params = { "token", tok, "data", data, "time", timeString };
if (changesHappened) {
String gcm = Preferences.getStringValue(GCMIntentService.PREF_REGISTRATION);
@ -206,7 +204,7 @@ public class ActFmInvoker {
if (SYNC_DEBUG)
Log.e("act-fm-post", request);
entity.addPart("token", new StringBody(tok));
entity.addPart("data", new StringBody(dataString));
entity.addPart("data", new StringBody(data));
entity.addPart("time", new StringBody(timeString));
String response = restClient.post(request, entity);

@ -40,7 +40,7 @@ import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.actfm.sync.messages.BriefMe;
import com.todoroo.astrid.actfm.sync.messages.ChangesHappened;
import com.todoroo.astrid.actfm.sync.messages.ClientToServerMessage;
import com.todoroo.astrid.actfm.sync.messages.JSONPayloadArray;
import com.todoroo.astrid.actfm.sync.messages.JSONPayloadBuilder;
import com.todoroo.astrid.actfm.sync.messages.NameMaps;
import com.todoroo.astrid.actfm.sync.messages.ReplayOutstandingEntries;
import com.todoroo.astrid.actfm.sync.messages.ReplayTaskListMetadataOutstanding;
@ -286,7 +286,7 @@ public class ActFmSyncThread {
}
if (!messageBatch.isEmpty() && checkForToken()) {
JSONPayloadArray payload = new JSONPayloadArray();
JSONPayloadBuilder payload = new JSONPayloadBuilder();
MultipartEntity entity = new MultipartEntity();
boolean containsChangesHappened = false;
for (int i = 0; i < messageBatch.size(); i++) {
@ -306,15 +306,14 @@ public class ActFmSyncThread {
messageBatch.clear();
continue;
}
System.err.println("PAYLOAD: " + payload.toString());
setupNotification();
payload.put(getClientVersion());
payload.addJSONObject(getClientVersion());
JSONArray errors = null;
try {
JSONObject response = actFmInvoker.postSync(payload, entity, containsChangesHappened, token);
JSONObject response = actFmInvoker.postSync(payload.closeAndReturnString(), entity, containsChangesHappened, token);
// process responses
String time = response.optString("time");
JSONArray serverMessagesJson = response.optJSONArray("messages");

@ -1,22 +1,33 @@
package com.todoroo.astrid.actfm.sync.messages;
import org.apache.http.entity.mime.MultipartEntity;
import org.json.JSONArray;
import org.json.JSONObject;
public class JSONPayloadArray extends JSONArray {
public class JSONPayloadBuilder {
private final StringBuilder sb = new StringBuilder("["); //$NON-NLS-1$
private final StringBuilder temp = new StringBuilder();
private int messageCount = 0;
public boolean addMessage(ClientToServerMessage<?> message, MultipartEntity entity) {
try {
JSONObject serialized = message.serializeToJSON(entity);
if (serialized != null) {
sb.append(serialized.toString())
return addJSONObject(serialized);
} catch (OutOfMemoryError e) {
return false;
}
}
public boolean addJSONObject(JSONObject obj) {
try {
temp.delete(0, temp.length());
if (obj != null) {
temp.append(obj)
.append(","); //$NON-NLS-1$
sb.append(temp);
messageCount++;
return true;
} else {
Loading…
Cancel
Save