From c9df29dbdaa98288c0f80af30c09d46be24cf9a5 Mon Sep 17 00:00:00 2001 From: hell Date: Mon, 7 Jul 2014 23:42:47 +0200 Subject: [PATCH] JSON fixes for Androdi 4.3 and lower --- .../github/nkzawa/socketio/client/Socket.java | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/github/nkzawa/socketio/client/Socket.java b/src/main/java/com/github/nkzawa/socketio/client/Socket.java index 579938b..a603c65 100644 --- a/src/main/java/com/github/nkzawa/socketio/client/Socket.java +++ b/src/main/java/com/github/nkzawa/socketio/client/Socket.java @@ -6,6 +6,7 @@ import com.github.nkzawa.socketio.parser.Packet; import com.github.nkzawa.socketio.parser.Parser; import com.github.nkzawa.thread.EventThread; import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; import java.util.*; @@ -46,6 +47,18 @@ public class Socket extends Emitter { put(EVENT_ERROR, 1); }}; + private static boolean jsonArrayNativeRemove; + + { + try { + jsonArrayNativeRemove = JSONArray.class.getMethod("remove", new Class[] { + int.class + }) != null; + } catch (NoSuchMethodException e) { + jsonArrayNativeRemove = false; + } + } + private boolean connected; private boolean disconnected = true; private int ids; @@ -55,7 +68,6 @@ public class Socket extends Emitter { private Queue subs; private final Queue> buffer = new LinkedList>(); - public Socket(Manager io, String nsp) { this.io = io; this.nsp = nsp; @@ -158,7 +170,12 @@ public class Socket extends Emitter { if (_args.get(_args.size() - 1) instanceof Ack) { logger.fine(String.format("emitting packet with ack id %d", Socket.this.ids)); Socket.this.acks.put(Socket.this.ids, (Ack)_args.remove(_args.size() - 1)); - jsonArgs.remove(jsonArgs.length() - 1); + if (jsonArrayNativeRemove) { + jsonArgs.remove(jsonArgs.length() - 1); + } else { + jsonArgs = remove(jsonArgs, jsonArgs.length() - 1); + packet.data = jsonArgs; + } packet.id = Socket.this.ids++; } @@ -168,6 +185,20 @@ public class Socket extends Emitter { return this; } + private static JSONArray remove(JSONArray a, int pos) { + JSONArray na = new JSONArray(); + try { + for (int i = 0; i < a.length(); i++){ + if (i != pos) { + na.put(a.get(i)); + } + } + } catch (Exception e) { + throw new JSONException(e); + } + return na; + } + /** * Emits an event with an acknowledge. * @@ -284,7 +315,7 @@ public class Socket extends Emitter { logger.fine(String.format("sending ack %s", args)); int type = HasBinaryData.hasBinary(args) ? Parser.BINARY_ACK : Parser.ACK; - Packet packet = new Packet(type, new JSONArray(args)); + Packet packet = new Packet(type, new JSONArray(Arrays.asList(args))); packet.id = id; self.packet(packet); }