Android 的 Office 365 SDK;如何列出文件夹下的文件?
Office 365 SDK for Android; How to list files under a folder?
Whosebug 的小伙伴们好,
我正在为 Android ( https://github.com/OfficeDev/Office-365-SDK-for-Android ), I've been looking on all SDK examples (https://msdn.microsoft.com/en-us/office/office365/howto/starter-projects-and-code-samples) 使用 Office-365-SDK,而且我一直在直接查看 SDK 源代码,但我不知道如何列出文件夹下的文件;所有示例仅在根文件夹下列出文件。
在 Office365 REST API 我可以清楚地看到有一个调用这个 pourpose ( https://msdn.microsoft.com/office/office365/APi/files-rest-operations#FolderoperationsListfoldercontentsREST ) 但在这个 SDK 上我没有找到创建相同调用的方法。
我的实际代码与调用时的 SDK 代码片段 (https://github.com/OfficeDev/O365-Android-Snippets/blob/master/app/src/main/java/com/microsoft/office365/snippetapp/Snippets/FileFolderSnippets.java) 完全相同 "getFilesAndFolders"。它在根目录下正确列出文件和文件夹,但我没有办法在具体文件夹下列出文件和文件夹,所以我无法创建文件资源管理器:(.
提前致谢!问候。
我找到了一种实现此目的的方法,虽然不是很优雅,但也许对某些人有所帮助,请查看此代码示例:
public ChildsSharePointClient getFileClient(String appendPath) {
if (mServices == null || mServices.isEmpty()) {
mCountDownLatch = new CountDownLatch(1);
discoverServices();
try {
mCountDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (mFileServiceInfo == null) {
mFileServiceInfo = getService(Constants.MYFILES_CAPABILITY);
}
String serviceEndpointUri = mFileServiceInfo.getserviceEndpointUri();
String serviceResourceId = mFileServiceInfo.getserviceResourceId();
if (!TextUtils.isEmpty(appendPath)) {
serviceEndpointUri += appendPath;
}
AuthenticationManager.getInstance().setResourceId(serviceResourceId);
DefaultDependencyResolver dependencyResolver = (DefaultDependencyResolver) AuthenticationManager.getInstance()
.getDependencyResolver();
return new ChildsSharePointClient(serviceEndpointUri, dependencyResolver);
}
public List<Item> getFilesList(String folderId) {
List<Item> filesAndFolders = null;
try {
if (TextUtils.isEmpty(folderId)) {
folderId = ROOT_PATH;
}
filesAndFolders = getFileClient(FILES_PATH + "/" + folderId)
.getChilds()
.read()
.get();
LOG.debug("Retrieved {} elements",filesAndFolders.size());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return filesAndFolders;
}
/**
* Created by mike on 16/06/15.
*/
public class ChildsSharePointClient extends SharePointClient {
/**
* Instantiates a new SharePointClient.
*
* @param url the url
* @param resolver the resolver
*/
public ChildsSharePointClient(String url, DependencyResolver resolver) {
super(url, resolver);
}
/**
* Gets Item.
*
* @return the Item
*/
public ODataCollectionFetcher<Item, ItemFetcher, ItemCollectionOperations> getChilds() {
return new ODataCollectionFetcher<Item, ItemFetcher,ItemCollectionOperations>("children", this, Item.class,ItemCollectionOperations.class);
}
}
基本上,我直接使用具有所需文件夹 ID 的 url 启动 SharePointClient,并且我已将 getChild() 操作添加到继承自 MS SharePointClient 的 class 并请求 "children"项。
希望它对某人有所帮助,同时我们找到了更优雅的解决方案。
正确的做法:
//retrieve the folder (as item)
Item item = client.getFiles().getOperations().getByPath("foo/path").get();
//get the folder, and get the children
client.getFiles().getById(item.getId()).asFolder().getChildren().read();
Microsoft 的 Marco Torres 在我在 SDK github -> https://github.com/OfficeDev/Office-365-SDK-for-Android/issues/88
上打开的一张票中回答了这个问题
希望对某人有所帮助:)
Whosebug 的小伙伴们好,
我正在为 Android ( https://github.com/OfficeDev/Office-365-SDK-for-Android ), I've been looking on all SDK examples (https://msdn.microsoft.com/en-us/office/office365/howto/starter-projects-and-code-samples) 使用 Office-365-SDK,而且我一直在直接查看 SDK 源代码,但我不知道如何列出文件夹下的文件;所有示例仅在根文件夹下列出文件。
在 Office365 REST API 我可以清楚地看到有一个调用这个 pourpose ( https://msdn.microsoft.com/office/office365/APi/files-rest-operations#FolderoperationsListfoldercontentsREST ) 但在这个 SDK 上我没有找到创建相同调用的方法。
我的实际代码与调用时的 SDK 代码片段 (https://github.com/OfficeDev/O365-Android-Snippets/blob/master/app/src/main/java/com/microsoft/office365/snippetapp/Snippets/FileFolderSnippets.java) 完全相同 "getFilesAndFolders"。它在根目录下正确列出文件和文件夹,但我没有办法在具体文件夹下列出文件和文件夹,所以我无法创建文件资源管理器:(.
提前致谢!问候。
我找到了一种实现此目的的方法,虽然不是很优雅,但也许对某些人有所帮助,请查看此代码示例:
public ChildsSharePointClient getFileClient(String appendPath) {
if (mServices == null || mServices.isEmpty()) {
mCountDownLatch = new CountDownLatch(1);
discoverServices();
try {
mCountDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (mFileServiceInfo == null) {
mFileServiceInfo = getService(Constants.MYFILES_CAPABILITY);
}
String serviceEndpointUri = mFileServiceInfo.getserviceEndpointUri();
String serviceResourceId = mFileServiceInfo.getserviceResourceId();
if (!TextUtils.isEmpty(appendPath)) {
serviceEndpointUri += appendPath;
}
AuthenticationManager.getInstance().setResourceId(serviceResourceId);
DefaultDependencyResolver dependencyResolver = (DefaultDependencyResolver) AuthenticationManager.getInstance()
.getDependencyResolver();
return new ChildsSharePointClient(serviceEndpointUri, dependencyResolver);
}
public List<Item> getFilesList(String folderId) {
List<Item> filesAndFolders = null;
try {
if (TextUtils.isEmpty(folderId)) {
folderId = ROOT_PATH;
}
filesAndFolders = getFileClient(FILES_PATH + "/" + folderId)
.getChilds()
.read()
.get();
LOG.debug("Retrieved {} elements",filesAndFolders.size());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return filesAndFolders;
}
/**
* Created by mike on 16/06/15.
*/
public class ChildsSharePointClient extends SharePointClient {
/**
* Instantiates a new SharePointClient.
*
* @param url the url
* @param resolver the resolver
*/
public ChildsSharePointClient(String url, DependencyResolver resolver) {
super(url, resolver);
}
/**
* Gets Item.
*
* @return the Item
*/
public ODataCollectionFetcher<Item, ItemFetcher, ItemCollectionOperations> getChilds() {
return new ODataCollectionFetcher<Item, ItemFetcher,ItemCollectionOperations>("children", this, Item.class,ItemCollectionOperations.class);
}
}
基本上,我直接使用具有所需文件夹 ID 的 url 启动 SharePointClient,并且我已将 getChild() 操作添加到继承自 MS SharePointClient 的 class 并请求 "children"项。
希望它对某人有所帮助,同时我们找到了更优雅的解决方案。
正确的做法:
//retrieve the folder (as item)
Item item = client.getFiles().getOperations().getByPath("foo/path").get();
//get the folder, and get the children
client.getFiles().getById(item.getId()).asFolder().getChildren().read();
Microsoft 的 Marco Torres 在我在 SDK github -> https://github.com/OfficeDev/Office-365-SDK-for-Android/issues/88
上打开的一张票中回答了这个问题希望对某人有所帮助:)