Home > OS >  Google Drive not showing all files in a directory
Google Drive not showing all files in a directory

Time:10-17

I'm new to JSON stuff and could not figure out why I'm having this issue, so here I am. I have a google drive folder that is publicly shared (https://drive.google.com/drive/folders/1RpFcoEWYsRt2MX7FotKcLICqYq6l5MFY). Here's the code I use to get entries (it's for testing atm):

new Thread(() -> {
        String sUrl = "https://www.googleapis.com/drive/v3/files?q='1RpFcoEWYsRt2MX7FotKcLICqYq6l5MFY' in parents&key=myKey";

        try {
            URL url = new URL(sUrl);
            URLConnection urlConnection = url.openConnection();
            urlConnection.connect();

            JsonParser jp = new JsonParser();
            JsonElement root = jp.parse(new InputStreamReader((InputStream) urlConnection.getContent()));
            JsonObject rootObj = root.getAsJsonObject();
            JsonArray files = rootObj.get("files").getAsJsonArray();

            for (JsonElement file : files) {
                String n = file.getAsJsonObject().get("name").getAsString();
                Log.e("amt", counter.getAndIncrement()   "");
                //if (n.toLowerCase().contains(name.toLowerCase())) ret.add(n);
            }

            ((HttpsURLConnection) urlConnection).disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }).start();

This code prints the numbers 0-99 in the console, always stopping at 99, despite the folder having more files than that. If anyone has a clue as to why this is happening, please let me know. Alternative methods are welcome as wel!

CodePudding user response:

In your situation, how about using pageSize to the query parameter? The default value of pageSize is 100. I thought that your issue might be due to this default value. So please modify your endpoint as follows and test it again.

From:

String sUrl = "https://www.googleapis.com/drive/v3/files?q='1RpFcoEWYsRt2MX7FotKcLICqYq6l5MFY' in parents&key=myKey";

To:

String sUrl = "https://www.googleapis.com/drive/v3/files?pageSize=1000&q='1RpFcoEWYsRt2MX7FotKcLICqYq6l5MFY' in parents&key=myKey";
  • pageSize=1000 was added to the endpoint.

Note:

  • If the number of files is over 1000, you can retrieve all file list using pageToken.

Reference:

  • Related