java - 如何从同步 RequestFuture 请求中获取响应代码
问题描述
我正在为应用程序使用 strava API。我正在发出同步请求,如下面的代码所示。
try {
RequestQueue queue = Volley.newRequestQueue(context);
RequestFuture<String> future = RequestFuture.newFuture();
StringRequest request = new StringRequest(Request.Method.GET, urlRequest, future, future);
queue.add(request);
dataResponse = dealWithResponse(future.get());
} catch (ExecutionException e) {
System.err.println(e.getLocalizedMessage());
System.err.println(e.getMessage());
System.err.println(e.toString());
} catch (java.lang.Exception e) {
e.printStackTrace();
}
我想知道发生错误时如何获取响应代码?例如,我请求的一些游乐设施已被删除/是私人的,我收到 404 错误代码。其他时候,我用完了 API 请求并获得了 403 的代码。我如何区分抛出的错误。
非常感谢您的帮助!
解决方案
在您处理的 catch 子句中,ExecutionException
您可以添加以下内容:
if (e.getCause() instanceof ClientError) {
ClientError error = (ClientError)e.getCause();
switch (error.networkResponse.statusCode) {
//Handle error code
}
}
推荐阅读
- angular - 如何在自己的页面上显示 Angular 中的子路由以进行正确的面包屑嵌套
- android - 如何在主片段区域之外过渡 UI 元素?
- java - bindingResult.hasErrors() 总是返回 false
- python - 如何检查发送的表情是否是 Discord 默认表情并禁止其他表情?
- ios - ios横向模式下flutter camerapreview旋转90度
- python - 尝试处理弹出 Selenium Python 时出现 NoAlertPresentException 错误
- html - 单击导航窗格到子页面时,相对路径会中断
- c++ - 更新 R 版本后模块中的 Rcpp 分段错误
- postgresql - 如何使用 pgloader 将特定的整个模式从一个 Postgres 数据库迁移到另一个?
- c++ - c ++列表在设置差异后不调整大小