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()); } }
文件评论接口是联网耗时接口,需要放在子线程中调用。