HMS Drive kit Query and query string

Viewed 95

I'm a beginner developer just starting with HMS. I'm trying to write a code that finds the "profile.json" file, which was previously uploaded to the cloud into the folder "DebtSorter". My code (Kotlin) for it currently looks like:

 var fileList: MutableList<File?> = mutableListOf()
        thread {
            try {
                val drive = buildDrive()
                val request = drive.files().list()
                var cursor: String?
                fileList = ArrayList()
                do {
                    var result = request.setQueryParam("fileName contains 'profile'")
                        .setOrderBy("fileName")
                        .setPageSize(10)
                        .setFields("*")
                        .execute()
                    for (file in result.files) {
                        fileList.add(file)
                    }
                    cursor = result.nextCursor
                    request.cursor = cursor
                } while (!StringUtils.isNullOrEmpty(cursor))
            } catch (e: java.lang.Exception) {
                Toast.makeText(applicationContext, "executeFilesList exception: $e", Toast.LENGTH_LONG)
                    .show()
            }
        }
        return fileList

However, the returned fileList is always empty, even when I upload the file to the root of the cloud. I think I'm messing something up with the query string, because I can upload files just fine. Can someone tell me the syntax and keywords used in the query string, and possibly help me with the code and how to search in a given folder in HMS Drive kit?

2 Answers

Update:

You can also refer to the demo: https://github.com/HMS-Core/hms-drive-serverdemo In the demo, the thread executes the method of obtaining the file list directly.


Your query parameters is incorrect. You can't define the format of your query string. Please refer to this sample query statement.

(assuming that the ID of folder A is f192358798744098816):

  1. To query all files in folder A, run the following statement: 'f192358798744098816' in parentFolder and mimeType != 'application/vnd.huawei-apps.folder'
  2. To query all folders in the recycle bin, run the following statement: mimeType='application/vnd.huawei-apps.folder' and recycled=true

The query criteria 'fileId' in parentFolder and recycled=true cannot be used together, that is, a user cannot query files or folders in a specified parent folder in the recycle bin.

For more details, see the official guide.

In the end, the problem was with creating the thread. Since the thread is asynchronous and I created the fileList variable before the thread, modified it in the thread and returned it after the thread, the actual modification happened after the method already returned with the empty fileList. I took out the lines with thread and put the method call into a thread. This solved it.

Related