适用于Android的Office 365 SDK;如何列出文件夹下的文件?

时间:2015-06-16 10:17:30

标签: android office365 onedrive

Hello StackOverflow好友,

我正在使用Office-365-SDK for Android(https://github.com/OfficeDev/Office-365-SDK-for-Android),我一直在查看所有SDK示例(https://msdn.microsoft.com/en-us/office/office365/howto/starter-projects-and-code-samples),而且我一直在寻找SDK源代码但我无法弄清楚如何列出文件夹下的文件;所有示例都只在根文件夹下进行文件列表。

在Office365 REST API上,我可以清楚地看到有关于此pourpose(https://msdn.microsoft.com/office/office365/APi/files-rest-operations#FolderoperationsListfoldercontentsREST)的调用,但在此SDK上我找不到创建相同调用的方法。

我的实际代码与调用“getFilesAndFolders”的SDK代码段(https://github.com/OfficeDev/O365-Android-Snippets/blob/master/app/src/main/java/com/microsoft/office365/snippetapp/Snippets/FileFolderSnippets.java)上的代码完全相同。它正确列出了root下的文件和文件夹,但我无法在具体文件夹下列出文件和文件夹,因此我无法创建文件资源管理器:(。

提前致谢!问候。

2 个答案:

答案 0 :(得分:1)

我找到了实现这个目标的方法,不是一个优雅的方法,但可能对某人有所帮助,请看一下这个代码示例:

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继承并请求“children”项目的类中。

希望有人帮助我们找到更优雅的解决方案。

答案 1 :(得分:1)

正确的方法:

//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();

微软的Marco Torres已经在我在SDK github上打开的一张票上回答了这个问题 - &gt; https://github.com/OfficeDev/Office-365-SDK-for-Android/issues/88

希望对某人有所帮助:)。