I'm not an experienced Java/Android developer and I'm trying to check if a file exists on Dropbox using Dropbox API for Android.

我不是一个经验丰富的Java / Android开发人员,我正在尝试使用适用于Android的Dropbox API检查Dropbox上是否存在文件。

I don't manage to find any method in the API to check this. The only way I found how to do this is to use try ... catch and catch the DropboxServerException, which in my opinion is very ugly.

我无法在API中找到任何方法来检查这一点。我发现如何做到这一点的唯一方法是使用try ... catch并捕获DropboxServerException,这在我看来非常难看。

Is there a nicer way, like if (file.exists), to check if the file exists ?

有没有更好的方法,比如if(file.exists),检查文件是否存在?

Thank you!

1 个解决方案

#1


0

Calling metadata and looking for an exception with a 404 response is probably the best way to check the existence of a single file in Dropbox.

调用元数据并使用404响应查找异常可能是检查Dropbox中是否存在单个文件的最佳方法。

Depending on what you're trying to do, there are likely better approaches:

根据您的尝试,可能有更好的方法:

  1. If you're enumerating all files anyway (e.g. using delta), then there's no need to ask the server whether a file exists, because you can keep track yourself.
  2. 如果你无论如何都要枚举所有文件(例如使用delta),那么就不需要询问服务器文件是否存在,因为你可以自己跟踪。

  3. If you're trying to read the file, go ahead and try to read it (and handle the exception if it doesn't exist).
  4. 如果您正在尝试读取该文件,请继续尝试读取它(如果不存在则处理异常)。

Note that this kind of (pseudo-)code is always buggy:

请注意,这种(伪)代码总是错误的:

if (file.exists()) {
    file.read();
}

What happens if the file is deleted in between when you check for its existence and when you try to read it? You'll get a 404 and an exception when you try to read the file. So you'll need code that handles that... which means that the first check for file existence doesn't do anything for you. You may as well skip it (and the corresponding round-trip to the server).

如果在检查文件存在和尝试读取文件之间删除文件会发生什么?当您尝试读取文件时,您将获得404和异常。因此,您将需要处理该代码的代码...这意味着对文件存在的第一次检查对您没有任何作用。您也可以跳过它(以及相应的服务器往返)。

更多相关文章

  1. android 关于读取SD卡或者U盘的一些方法
  2. 【Android笔记】Activity涉及界面全屏的方法
  3. Android中WebView实现Javascript调用Java类方法
  4. Android中的内存储、外存储概念、文件操作与PC端的有些不同
  5. 由Android架构图所想到的Android开发学习方向方法
  6. Android 打包VersionCode自增、APK文件名修改、上传蒲公英
  7. SharedPreference.Editor的apply和commit方法异同
  8. Android无法加载第三方so的解决方法
  9. Android开发-直播视讯(3)-创建一个Ubuntu虚拟机并实现VMtools文

随机推荐

  1. Android中声音处理流程
  2. android中重写onTouchEvent分别实现点击
  3. android渲染网页时onPageFinished不触发
  4. Android 自定义View 横向翻页
  5. Android圆环形自定义进度条控件的绘制
  6. Android软键盘弹出不影响布局的方法
  7. android查看每个app的内存占用情况
  8. android service 实例
  9. 修改Android Studio的字体
  10. android仿ios弹性页