我正在尝试使用 Chrome 自定义选项卡打开 PDF 文件。但是,我知道 PDF 仅在特定时间段内可用,而在其他时间则返回 404 错误。所以我开始寻找某种方法来处理返回的 404 错误,但我不知道可以从哪里检索 HTML 状态代码。
这是我的代码:
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent intent = builder.build();
builder.setToolbarColor(getResources().getColor(R.color.md_red_700));
builder.setStartAnimations(WelcomeActivity.this, R.anim.slide_in_right, R.anim.slide_out_left);
builder.setExitAnimations(WelcomeActivity.this, R.anim.slide_in_left, R.anim.slide_out_right);
intent.launchUrl(WelcomeActivity.this, Uri.parse(completeUrl));
tl;dr: 我想在打开 chrome customTabs 时管理 404 错误,我该如何执行此操作?
最佳答案
在启动 PDF URL 之前,您首先需要调用 HTTPURL Connection 来获取状态。下面的代码可帮助您检查 PDF URL 的状态。
一旦获得成功代码 200,然后调用 CustomIntentTab。
下面的代码可以帮助您检查状态代码。
URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
关于java - 使用 Chrome 自定义选项卡时如何捕获错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48048041/