public static BNWMessage initFromJSON(JSONObject json) throws JSONException { BNWMessage jmsg = new BNWMessage(); if (json.has("message")) { jmsg.setMID(new BnwMessageID(json.getString("message"))); jmsg.setRIDString(json.getString("id")); } else { jmsg.setMID(new BnwMessageID(json.getString("id"))); } jmsg.Text = json.getString("text"); Calendar cal = new GregorianCalendar(); cal.setTimeZone(TimeZone.getTimeZone("GMT")); cal.setTimeInMillis((long) (json.getDouble("date") * 1000)); jmsg.Timestamp = cal.getTime(); jmsg.User = new JuickUser(); jmsg.User.UName = json.getString("user"); if (json.has("replyto") && json.get("replyto") != JSONObject.NULL) jmsg.setReplyToString(json.getString("replyto")); if (json.has("tags")) { JSONArray tags = json.getJSONArray("tags"); for (int n = 0; n < tags.length(); n++) { jmsg.tags.add(tags.getString(n).replace(""", "\"")); } } if (json.has("clubs")) { JSONArray clubs = json.getJSONArray("clubs"); for (int n = 0; n < clubs.length(); n++) { jmsg.clubs.add(clubs.getString(n).replace(""", "\"")); } } if (json.has("replycount")) { jmsg.replies = json.getInt("replycount"); } jmsg.microBlogCode = BnwMessageID.CODE; return jmsg; }
@Override public void getChildren( MessageID mid, Utils.Notification notification, Utils.Function<Void, ArrayList<JuickMessage>> cont) { String midString = ((BnwMessageID) mid).getId(); final String jsonStr = httpClientService .getJSON( "http://ipv4.bnw.im/api/show?message=" + midString + "&replies=1", notification) .getResult(); if (jsonStr != null) { try { JSONObject fullThread = new JSONObject(jsonStr); JSONObject root = fullThread.getJSONObject("message"); ArrayList<BNWMessage> msgs = new ArrayList<BNWMessage>(); msgs.add(initFromJSON(root)); JSONArray replies = fullThread.getJSONArray("replies"); HashMap<String, Integer> numbersRemap = new HashMap<String, Integer>(); int replyNo = 1; for (int i = 0; i < replies.length(); i++) { BNWMessage reply = initFromJSON(replies.getJSONObject(i)); msgs.add(reply); reply.setRID(replyNo); numbersRemap.put(reply.getRIDString(), replyNo); replyNo++; } for (int i = 1; i < msgs.size(); i++) { BNWMessage msg = msgs.get(i); String replyToString = msg.getReplyToString(); if (replyToString == null) { msg.setReplyTo(0); } else { Integer prevComment = numbersRemap.get(replyToString); if (prevComment == null) prevComment = 0; msg.setReplyTo(prevComment); } } cont.apply(new ArrayList<JuickMessage>(msgs)); } catch (JSONException e) { cont.apply(new ArrayList<JuickMessage>()); } } else { cont.apply(new ArrayList<JuickMessage>()); } }