如何处理 Dropbox 中的错误 java API
How to deal with errors in the Dropbox java API
在尝试开发与某些云服务交互的应用程序时,我发现 Java 的 Dropbox API 特别令人困惑。
具体如何查找HTTP错误。例如,使用 Google Drive API 如果请求失败,将抛出 IOException,但是您可以将该 IOException 解析为 GoogleJsonResponseException,然后您可以提取状态代码。
try {
File f =drive.files().create(fileMetadata).setFields("id").execute();
return f.getId();
} catch (IOException e) {
if (e instanceof GoogleJsonResponseException){
int statusCode = ((GoogleJsonResponseException) e).getStatusCode();
errorHandler(statusCode);
} else {
e.printStackTrace();
}
}
java dropbox API 中是否有类似的东西(特别是 3.0.5)。
我一直在环顾四周,似乎没有,但我想在我进入上面提到的极其专业和复杂的编码的兔子洞之前确定 here。如果有人可以给我一个例子,说明我如何正确处理这些异常。
[交叉链接供参考:https://www.dropboxforum.com/t5/API-support/How-to-get-error-responses-java-api/m-p/258338#M14989]
Dropbox Java SDK 会自动将 HTTPS 响应中的结构化错误数据解析为本机 类,您可以根据需要使用或多或少的特定信息。结构化错误响应(通常在响应正文中 JSON)提供比状态代码更多的粒度。
例如,您链接到 my post 的 Whosebug 文档中现在缺少以下代码示例:
try {
SharedLinkMetadata sharedLinkMetadata = client.sharing().createSharedLinkWithSettings("/test.txt");
System.out.println(sharedLinkMetadata.getUrl());
} catch (CreateSharedLinkWithSettingsErrorException ex) {
System.out.println(ex);
} catch (DbxException ex) {
System.out.println(ex);
}
和 here's an example 显示如何检查特定错误情况(即该示例中的 "path" 错误)。
这是处理这些异常的推荐方式。我不相信 SDK 提供了一种方法来检索原始的未解析的错误响应。 (如果您不想使用 SDK,您可以直接调用 the HTTPS endpoints。)
在尝试开发与某些云服务交互的应用程序时,我发现 Java 的 Dropbox API 特别令人困惑。
具体如何查找HTTP错误。例如,使用 Google Drive API 如果请求失败,将抛出 IOException,但是您可以将该 IOException 解析为 GoogleJsonResponseException,然后您可以提取状态代码。
try {
File f =drive.files().create(fileMetadata).setFields("id").execute();
return f.getId();
} catch (IOException e) {
if (e instanceof GoogleJsonResponseException){
int statusCode = ((GoogleJsonResponseException) e).getStatusCode();
errorHandler(statusCode);
} else {
e.printStackTrace();
}
}
java dropbox API 中是否有类似的东西(特别是 3.0.5)。 我一直在环顾四周,似乎没有,但我想在我进入上面提到的极其专业和复杂的编码的兔子洞之前确定 here。如果有人可以给我一个例子,说明我如何正确处理这些异常。
[交叉链接供参考:https://www.dropboxforum.com/t5/API-support/How-to-get-error-responses-java-api/m-p/258338#M14989]
Dropbox Java SDK 会自动将 HTTPS 响应中的结构化错误数据解析为本机 类,您可以根据需要使用或多或少的特定信息。结构化错误响应(通常在响应正文中 JSON)提供比状态代码更多的粒度。
例如,您链接到 my post 的 Whosebug 文档中现在缺少以下代码示例:
try {
SharedLinkMetadata sharedLinkMetadata = client.sharing().createSharedLinkWithSettings("/test.txt");
System.out.println(sharedLinkMetadata.getUrl());
} catch (CreateSharedLinkWithSettingsErrorException ex) {
System.out.println(ex);
} catch (DbxException ex) {
System.out.println(ex);
}
和 here's an example 显示如何检查特定错误情况(即该示例中的 "path" 错误)。
这是处理这些异常的推荐方式。我不相信 SDK 提供了一种方法来检索原始的未解析的错误响应。 (如果您不想使用 SDK,您可以直接调用 the HTTPS endpoints。)