gpt4 book ai didi

java - 在Java中插入没有parentId的Google Drive文件

转载 作者:行者123 更新时间:2023-12-02 08:33:25 34 4
gpt4 key购买 nike

要将文件上传到云端硬盘中的特定文件夹,我知道我必须在以下方法中设置参数parentId:

  File body = new File();
body.setParents(Arrays.asList(new ParentReference().setId(parentId)));

现在,有没有办法使用不同的参数?我的问题是,当我使用 Gson 解析器从 Drive 中的特定文件夹检索资源时,parentId 属性始终为 null。为什么会发生这种情况?上传的时候需要设置Id吗?如果我直接在drive.google.com 站点中创建文件夹会怎样?其他参数(例如 title 或 dateCreated)工作正常。

非常感谢。

编辑:我添加了一些代码来显示发生了什么。

MyJson.java

@SerializedName("parentId")
private String parentId;

@SerializedName("createdDate")
private String createdDate;

public final String getDateCreated(){
return this.createdDate;
}

public final String getParentId(){
return this.parentId;
}

Main.java

public static String[] jsonParser(String jsonText){

Gson gson = new Gson();

JsonParser parser = new JsonParser();

JsonArray Jarray = parser.parse(jsonText).getAsJsonArray();

ArrayList<MyJson> lcs = new ArrayList<MyJson>();

StringBuilder sb = new StringBuilder();
//Please ignore this Stringbuilder is just for testing purposes
for(JsonElement obj : Jarray )
{
MyJson cse = gson.fromJson( obj , MyJson.class);
lcs.add(cse);
}

List<String> strings = new ArrayList<String>();

for(MyJson j: lcs){
strings.add(j.getTitle() );
sb.append(j.getParentId() + " " + j.getDateCreated());
}

System.out.println(sb.toString()); //Here it prints out null+date


String[] arr = strings.toArray(new String[strings.size()]);

//When I print this array shows the title perfectly
return arr;

}

这是我传递给上一个方法的文本资源。

[[{"alternateLink":"https://docs.google.com/folderview?id=0Byylok4NwwGOR2VTX1JOb1BnNDg&usp=drivesdk","appDataContents":false,"createdDate":"2013-07-25T06:17:20.228Z","editable":true,"etag":"\"RFvxxXV9yoZniidCHgcusodAlXI/MTM3NDczMzA0MDIyOA\"","iconLink":"https://ssl.gstatic.com/docs/doclist/images/icon_11_collection_list.png","id":"0Byylok4NwwGOR2VTX1JOb1BnNDg","kind":"drive#file","labels":{"hidden":false,"restricted":false,"starred":false,"trashed":false,"viewed":false},"lastModifyingUser":{"displayName":"Luis Manuel Lavieri","isAuthenticatedUser":true,"kind":"drive#user","permissionId":"13217068352953415214","picture":{"url":"https://lh3.googleusercontent.com/-yNHuP50oJzI/AAAAAAAAAAI/AAAAAAAAAIQ/Wv4lZdE3FyU/s64/photo.jpg"}},"lastModifyingUserName":"Luis Manuel Lavieri","mimeType":"application/vnd.google-apps.folder","modifiedDate":"2013-07-25T06:17:20.228Z","ownerNames":["Luis Manuel Lavieri"],"owners":[{"displayName":"Luis Manuel Lavieri","isAuthenticatedUser":true,"kind":"drive#user","permissionId":"13217068352953415214","picture":{"url":"https://lh3.googleusercontent.com/-yNHuP50oJzI/AAAAAAAAAAI/AAAAAAAAAIQ/Wv4lZdE3FyU/s64/photo.jpg"}}],"parents":[{"id":"0ACylok4NwwGOUk9PVA","isRoot":true,"kind":"drive#parentReference","parentLink":"https://www.googleapis.com/drive/v2/files/0ACylok4NwwGOUk9PVA","selfLink":"https://www.googleapis.com/drive/v2/files/0Byylok4NwwGOR2VTX1JOb1BnNDg/parents/0ACylok4NwwGOUk9PVA"}],"quotaBytesUsed":"0","selfLink":"https://www.googleapis.com/drive/v2/files/0Byylok4NwwGOR2VTX1JOb1BnNDg","shared":false,"title":"Web Design","userPermission":{"etag":"\"RFvxxXV9yoZniidCHgcusodAlXI/Rok55oKCdYb_mqyTjquQPLiUiIg\"","id":"me","kind":"drive#permission","role":"owner","selfLink":"https://www.googleapis.com/drive/v2/files/0Byylok4NwwGOR2VTX1JOb1BnNDg/permissions/me","type":"user"},"writersCanShare":true}, {"alternateLink":"https://docs.google.com/folderview?id=0Byylok4NwwGORzhjODJPeVVUYXM&usp=drivesdk","appDataContents":false,"createdDate":"2013-07-25T06:17:10.835Z","editable":true,"etag":"\"RFvxxXV9yoZniidCHgcusodAlXI/MTM3NDczMzAzMDgzNQ\"","iconLink":"https://ssl.gstatic.com/docs/doclist/images/icon_11_collection_list.png","id":"0Byylok4NwwGORzhjODJPeVVUYXM","kind":"drive#file","labels":{"hidden":false,"restricted":false,"starred":false,"trashed":false,"viewed":false},"lastModifyingUser":{"displayName":"Luis Manuel Lavieri","isAuthenticatedUser":true,"kind":"drive#user","permissionId":"13217068352953415214","picture":{"url":"https://lh3.googleusercontent.com/-yNHuP50oJzI/AAAAAAAAAAI/AAAAAAAAAIQ/Wv4lZdE3FyU/s64/photo.jpg"}},"lastModifyingUserName":"Luis Manuel Lavieri","mimeType":"application/vnd.google-apps.folder","modifiedDate":"2013-07-25T06:17:10.835Z","ownerNames":["Luis Manuel Lavieri"],"owners":[{"displayName":"Luis Manuel Lavieri","isAuthenticatedUser":true,"kind":"drive#user","permissionId":"13217068352953415214","picture":{"url":"https://lh3.googleusercontent.com/-yNHuP50oJzI/AAAAAAAAAAI/AAAAAAAAAIQ/Wv4lZdE3FyU/s64/photo.jpg"}}],"parents":[{"id":"0ACylok4NwwGOUk9PVA","isRoot":true,"kind":"drive#parentReference","parentLink":"https://www.googleapis.com/drive/v2/files/0ACylok4NwwGOUk9PVA","selfLink":"https://www.googleapis.com/drive/v2/files/0Byylok4NwwGORzhjODJPeVVUYXM/parents/0ACylok4NwwGOUk9PVA"}],"quotaBytesUsed":"0","selfLink":"https://www.googleapis.com/drive/v2/files/0Byylok4NwwGORzhjODJPeVVUYXM","shared":false,"title":"Finance","userPermission":{"etag":"\"RFvxxXV9yoZniidCHgcusodAlXI/Hsb4mdHUJCfDZwXE6ivhuygOXig\"","id":"me","kind":"drive#permission","role":"owner","selfLink":"https://www.googleapis.com/drive/v2/files/0Byylok4NwwGORzhjODJPeVVUYXM/permissions/me","type":"user"},"writersCanShare":true}, {"alternateLink":"https://docs.google.com/folderview?id=0Byylok4NwwGOa1E5Zng1dTVpZ0U&usp=drivesdk","appDataContents":false,"createdDate":"2013-07-25T06:17:00.482Z","editable":true,"etag":"\"RFvxxXV9yoZniidCHgcusodAlXI/MTM3NDczMzAyMDQ4Mg\"","iconLink":"https://ssl.gstatic.com/docs/doclist/images/icon_11_collection_list.png","id":"0Byylok4NwwGOa1E5Zng1dTVpZ0U","kind":"drive#file","labels":{"hidden":false,"restricted":false,"starred":false,"trashed":false,"viewed":false},"lastModifyingUser":{"displayName":"Luis Manuel Lavieri","isAuthenticatedUser":true,"kind":"drive#user","permissionId":"13217068352953415214","picture":{"url":"https://lh3.googleusercontent.com/-yNHuP50oJzI/AAAAAAAAAAI/AAAAAAAAAIQ/Wv4lZdE3FyU/s64/photo.jpg"}},"lastModifyingUserName":"Luis Manuel Lavieri","mimeType":"application/vnd.google-apps.folder","modifiedDate":"2013-07-25T06:17:00.482Z","ownerNames":["Luis Manuel Lavieri"],"owners":[{"displayName":"Luis Manuel Lavieri","isAuthenticatedUser":true,"kind":"drive#user","permissionId":"13217068352953415214","picture":{"url":"https://lh3.googleusercontent.com/-yNHuP50oJzI/AAAAAAAAAAI/AAAAAAAAAIQ/Wv4lZdE3FyU/s64/photo.jpg"}}],"parents":[{"id":"0ACylok4NwwGOUk9PVA","isRoot":true,"kind":"drive#parentReference","parentLink":"https://www.googleapis.com/drive/v2/files/0ACylok4NwwGOUk9PVA","selfLink":"https://www.googleapis.com/drive/v2/files/0Byylok4NwwGOa1E5Zng1dTVpZ0U/parents/0ACylok4NwwGOUk9PVA"}],"quotaBytesUsed":"0","selfLink":"https://www.googleapis.com/drive/v2/files/0Byylok4NwwGOa1E5Zng1dTVpZ0U","shared":false,"title":"Marketing","userPermission":{"etag":"\"RFvxxXV9yoZniidCHgcusodAlXI/1meV_djX_pEtNrpJb1GQPQAT5hw\"","id":"me","kind":"drive#permission","role":"owner","selfLink":"https://www.googleapis.com/drive/v2/files/0Byylok4NwwGOa1E5Zng1dTVpZ0U/permissions/me","type":"user"},"writersCanShare":true}]]

我知道那里有各种类型的id,而不是我正在寻找的(parentId,folderId),但是如果我想在“Web Design”文件夹中插入文件我该怎么办?我该如何设置这些 Id?我应该如何处理空值?

最佳答案

就像设置父对象一样,使用父对象列表,您应该以相同的方式读取它。

"parents": [
parents Resource
],

不是父 ID。

关于java - 在Java中插入没有parentId的Google Drive文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17868084/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com