public ArrayList<JuickMessage> parseAPIPostAndReplies(String jsonStr) {
   ArrayList<JuickMessage> retval = new ArrayList<JuickMessage>();
   try {
     JSONObject jo = new JSONObject(new JSONTokener(new FastStringReader(jsonStr)));
     JSONObject post = jo.getJSONObject("post");
     PointMessage msg = new PointMessage();
     parsePointAPIMessagePost(msg, post);
     retval.add(msg);
     JSONArray comments = jo.getJSONArray("comments");
     for (int i = 0; i < comments.length(); i++) {
       JSONObject comment = comments.getJSONObject(i);
       PointMessage comm = new PointMessage();
       try {
         parsePointAPIComment(comment, comm, msg);
         retval.add(comm);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   } catch (JSONException e) {
     e.printStackTrace();
   } catch (ParseException e) {
     e.printStackTrace();
   }
   return retval;
 }
 private void parsePointAPIMessage(JSONObject post, PointMessage msg)
     throws JSONException, ParseException {
   JSONObject p = post.getJSONObject("post");
   parsePointAPIMessagePost(msg, p);
   PointMessageID mid = (PointMessageID) msg.getMID();
   mid.uid = post.getInt("uid");
   msg.source = post.toString();
   msg.subscribed = post.getBoolean("subscribed");
 }
 /**
  * Get an array of field names from a JSONObject.
  *
  * @return An array of field names, or null if there are no names.
  */
 public static String[] getNames(JSONObject jo) {
   int length = jo.length();
   if (length == 0) {
     return null;
   }
   Iterator<String> iterator = jo.keys();
   String[] names = new String[length];
   int i = 0;
   while (iterator.hasNext()) {
     names[i] = iterator.next();
     i += 1;
   }
   return names;
 }
 static final Writer writeValue(Writer writer, Object value, int indentFactor, int indent)
     throws JSONException, IOException {
   if (value == null || value.equals(null)) {
     writer.write("null");
   } else if (value instanceof JSONObject) {
     ((JSONObject) value).write(writer, indentFactor, indent);
   } else if (value instanceof JSONArray) {
     ((JSONArray) value).write(writer, indentFactor, indent);
   } else if (value instanceof Double) {
     writer.write(numberToString((Double) value));
   } else if (value instanceof Float) {
     writer.write(numberToString((Float) value));
   } else if (value instanceof Integer || value instanceof Long) {
     writer.write(value.toString());
   } else if (value instanceof Boolean) {
     writer.write(value.toString());
   } else if (value instanceof JSONString) {
     Object o;
     try {
       o = ((JSONString) value).toJSONString();
     } catch (Exception e) {
       throw new JSONException(e);
     }
     writer.write(o != null ? o.toString() : quote(value.toString()));
   } else {
     quote(value.toString(), writer);
   }
   return writer;
 }
 @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>());
   }
 }
 /**
  * Construct a JSONObject from a subset of another JSONObject. An array of strings is used to
  * identify the keys that should be copied. Missing keys are ignored.
  *
  * @param jo A JSONObject.
  * @param names An array of strings.
  * @throws JSONException
  * @exception JSONException If a value is a non-finite number or if a name is duplicated.
  */
 public JSONObject(JSONObject jo, String[] names) {
   this();
   for (int i = 0; i < names.length; i += 1) {
     try {
       this.putOnce(names[i], jo.opt(names[i]));
     } catch (Exception ignore) {
     }
   }
 }
 public ArrayList<JuickMessage> parseJSONpure(String jsonStr, boolean storeSource) {
   ArrayList<JuickMessage> messages = new ArrayList<JuickMessage>();
   if (jsonStr != null) {
     try {
       JSONObject objMessages = new JSONObject(jsonStr);
       JSONArray json = objMessages.getJSONArray("messages");
       int cnt = json.length();
       for (int i = 0; i < cnt; i++) {
         JSONObject jsonObject = json.getJSONObject(i);
         JuickMessage msg = initFromJSON(jsonObject);
         messages.add(msg);
         if (!storeSource) msg.source = null;
       }
     } catch (Exception e) {
       e.printStackTrace();
       // Log.e("initOpinionsAdapter", e.toString());
     }
   }
   return messages;
 }
 public ArrayList<JuickMessage> parseAPIMessageListPure(String jsonStr) {
   ArrayList<JuickMessage> retval = new ArrayList<JuickMessage>();
   try {
     JSONObject jo = new JSONObject(new JSONTokener(new FastStringReader(jsonStr)));
     JSONArray posts = jo.getJSONArray("posts");
     for (int i = 0; i < posts.length(); i++) {
       JSONObject post = posts.getJSONObject(i);
       PointMessage msg = new PointMessage();
       try {
         parsePointAPIMessage(post, msg);
         retval.add(msg);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   } catch (JSONException e) {
     e.printStackTrace();
   }
   return retval;
 }
  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("&quot;", "\""));
      }
    }
    if (json.has("clubs")) {
      JSONArray clubs = json.getJSONArray("clubs");
      for (int n = 0; n < clubs.length(); n++) {
        jmsg.clubs.add(clubs.getString(n).replace("&quot;", "\""));
      }
    }
    if (json.has("replycount")) {
      jmsg.replies = json.getInt("replycount");
    }
    jmsg.microBlogCode = BnwMessageID.CODE;
    return jmsg;
  }
 private void parsePointAPIComment(JSONObject comm, PointMessage msg, PointMessage parent)
     throws JSONException, ParseException {
   PointUser pu = new PointUser();
   msg.User = pu;
   JSONObject author = comm.getJSONObject("author");
   pu.UName = author.getString("login");
   pu.FullName = author.getString("name");
   pu.UID = author.getInt("id");
   if (author.has("avatar") && author.get("avatar") != JSONObject.NULL) {
     pu.avatarUrl = author.getString("avatar");
   }
   msg.Text = comm.getString("text");
   msg.is_rec = comm.getBoolean("is_rec");
   msg.Timestamp = parsePointAPIDate(comm.getString("created"));
   msg.setMID(parent.getMID());
   msg.setRID(comm.getInt("id"));
   Object toCommentId = comm.get("to_comment_id");
   if (toCommentId != JSONObject.NULL) {
     msg.setReplyTo(comm.getInt("to_comment_id"));
   }
   msg.microBlogCode = PointMessageID.CODE;
 }
 public void parsePointAPIMessagePost(PointMessage msg, JSONObject p)
     throws JSONException, ParseException {
   PointUser pu = new PointUser();
   msg.User = pu;
   JSONObject author = p.getJSONObject("author");
   pu.UName = author.getString("login");
   pu.FullName = author.getString("name");
   pu.UID = author.getInt("id");
   if (author.has("avatar") && author.get("avatar") != JSONObject.NULL)
     pu.avatarUrl = author.getString("avatar");
   msg.Text = p.getString("text");
   String createdStr = p.getString("created");
   msg.Timestamp = parsePointAPIDate(createdStr);
   msg.tags = new Vector<String>();
   if (p.has("tags")) {
     JSONArray tagso = p.getJSONArray("tags");
     for (int i = 0; i < tagso.length(); i++) {
       msg.tags.add(tagso.getString(i));
     }
   }
   if (p.has("files")) {
     JSONArray fileso = p.getJSONArray("files");
     for (int i = 0; i < fileso.length(); i++) {
       msg.Text += "\n@\n" + fileso.getString(i);
     }
   }
   msg.setMID(new PointMessageID(pu.UName, p.getString("id"), 0));
   msg.replies = p.getInt("comments_count");
   msg.microBlogCode = PointMessageID.CODE;
 }