目录

文件评论

场景介绍

华为云空间服务支持用户对云端文件进行评论,并对评论进行查看、更新和删除操作。

开发前提

1、请确认您已完成开发准备。

2、在进行开发之前,您可以下载华为云空间客户端示例代码进行快速体验。

构造Drive对象

调用Drive.Builder.build接口构造Drive对象。
private Drive buildDrive() {  
    Drive service = new Drive.Builder(CredentialManager.getInstance().getCredential(), context).build();  
    return service;  
}

创建文件评论

调用Comments.Create接口创建文件评论:

/**
 * 创建文件评论
 *
 * @param fileId 文件id
 */
private Comment createComments(String fileId) {
    Comment comment = null;
    Drive drive = buildDrive();
    try {
        Comment content = new Comment();
        content.setDescription("interface test");
        content.setCreatedTime(new DateTime(System.currentTimeMillis()));
        comment = drive.comments().create(fileId, content).setFields("*").execute();
    } catch (Exception ex) {
        Log.e(TAG, "createComments error: " + ex.toString());
    }
    return comment;
}

获取文件评论列表

调用Comments.List接口获取文件评论列表:

/**
 * 获取文件评论列表
 *
 * @param fileId 文件id
 */
private List<Comment> listComments(String fileId) {
    Drive drive = buildDrive();
    ArrayList<Comment> commentArrayList = new ArrayList<>();
    String nextCursor = null;
    try {
        Drive.Comments.List request = drive.comments().list(fileId);
        do {
            if (nextCursor != null) {
                request.setCursor(nextCursor);
            }
            CommentList commentList = request.setPageSize(100).setFields("*").execute();
            ArrayList<Comment> comments = (ArrayList<Comment>) commentList.getComments();
            if (comments == null) {
                break;
            }
            commentArrayList.addAll(comments);
            nextCursor = commentList.getNextCursor();
        } while (!StringUtils.isNullOrEmpty(nextCursor));
    } catch (IOException e) {
        Logger.e(TAG, "comments list error: " + e.toString());
    }
    return commentArrayList;
}

获取文件评论详情

调用Comments.Get接口获取文件评论详情:

/**
 * 获取文件评论详情
 *
 * @param fileId 文件id
 * @param commentId 评论id
 */
private Comment getComments(String fileId, String commentId) {
    Comment comment = null;
    Drive drive = buildDrive();
    try {
        comment = drive.comments().get(fileId, commentId).setFields("*").execute();
        Log.d(TAG, "comment: " + comment.getDescription());
    } catch (Exception ex) {
        Log.e(TAG, "getComments error: " + ex.toString());
    }
    return comment;
}

更新文件评论

调用Comments.Update接口更新文件评论:

/**
 * 更新文件评论
 *
 * @param fileId 文件id
 * @param commentId 评论id
 */
private void updateComments(String fileId, String commentId) {
    Drive drive = buildDrive();
    try {
        Comment content = new Comment();
        content.setDescription("update interface test");
        drive.comments().update(fileId, commentId, content).execute();
    } catch (Exception ex) {
        Log.e(TAG, "updateComments error: " + ex.toString());
    }
}

删除文件评论

调用Comments.Delete接口删除文件评论:

/**
 * 删除文件评论
 *
 * @param fileId 文件id
 * @param commentId 评论id
 */
private void deleteComments(String fileId, String commentId) {
    Drive drive = buildDrive();
    try {
        drive.comments().delete(fileId, commentId).execute();
    } catch (Exception ex) {
        Log.e(TAG, "deleteComments error: " + ex.toString());
    }
}

文件评论接口是联网耗时接口,需要放在子线程中调用。