要将文件上传到驱动器中的特定文件夹,我知道我必须在以下方法中设置参数 parentId:
File body = new File();
body.setParents(Arrays.asList(new ParentReference().setId(parentId)));
现在,有没有办法使用不同的参数?我的问题是,当我使用 Gson 解析器从 Drive 中的特定文件夹检索资源时,parentId 属性始终为空。为什么会这样?上传的时候需要设置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?我应该如何处理空值?