要将文件上传到云端硬盘中的特定文件夹,我了解到必须使用以下方法设置参数parentId:
File body = new File();
body.setParents(Arrays.asList(new ParentReference().setId(parentId)));
现在,有没有办法使用不同的参数?我的问题是,当我使用my 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?我应该怎么处理空值?
答案 0 :(得分:0)
就像您使用父对象列表设置父对象一样,您应该以相同的方式阅读它。
"parents": [
parents Resource
],
不是parentId。