示例#1
0
  private void parseCommentsJSON(InputStream in) throws IOException, JsonParseException {
    int insertedCommentIndex;
    String genericListingError = "Not a comments listing";
    try {
      Listing[] listings = mObjectMapper.readValue(in, Listing[].class);

      // listings[0] is a thread Listing for the OP.
      // process same as a thread listing more or less

      Assert.assertEquals(Constants.JSON_LISTING, listings[0].getKind(), genericListingError);

      // Save modhash, ignore "after" and "before" which are meaningless in this context (and
      // probably null)
      ListingData threadListingData = listings[0].getData();
      if (StringUtils.isEmpty(threadListingData.getModhash())) mSettings.setModhash(null);
      else mSettings.setModhash(threadListingData.getModhash());

      if (Constants.LOGGING)
        Log.d(TAG, "Successfully got OP listing[0]: modhash " + mSettings.getModhash());
      ThingListing threadThingListing = threadListingData.getChildren()[0];
      Assert.assertEquals(Constants.THREAD_KIND, threadThingListing.getKind(), genericListingError);
      // listings[1] is a comment Listing for the comments
      ListingData commentListingData = listings[1].getData();
      if (isInsertingEntireThread()) {
        parseOP(
            threadThingListing.getData(),
            commentListingData.getChildren().length == 0
                ? null
                : commentListingData.getChildren()[0].getData());
        insertedCommentIndex = 0; // we just inserted the OP into position 0
        if (!StringUtils.isEmpty(mJumpToCommentId) && mJumpToCommentContext > 0) {
          // If viewing context, then the 'first' item will be the context-viewing warning.
          insertedCommentIndex++;
        }

        // at this point we've started displaying comments, so disable the loading screen
        disableLoadingScreenKeepProgress();
      } else {
        insertedCommentIndex = mPositionOffset - 1; // -1 because we +1 for the first comment
      }

      // Go through the children and get the ThingInfos
      for (ThingListing commentThingListing : commentListingData.getChildren()) {
        // insert the comment and its replies, prefix traversal order
        insertedCommentIndex =
            insertNestedComment(commentThingListing, 0, insertedCommentIndex + 1);
      }

      mProcessCommentsTask.mergeLowPriorityListToMainList();

    } catch (Exception ex) {
      if (Constants.LOGGING) Log.e(TAG, "parseCommentsJSON", ex);
    }
  }