使用 Chrome customTabs 时如何捕获错误

How to catch errors when using Chrome customTabs

我正在尝试使用 Chrome customTab 打开 PDF 文件。但是,我知道 PDF 仅在特定时间段可用,而 returns 在其他时间段出现 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 错误,我该怎么做?

在打开你的 customChromeTab.

之前,只需使用一些 library 来获得 status code

通过这样做,您可以检查 4o4。

希望这个回答对您有所帮助。

在启动 PDF Url 之前,您首先需要为状态调用 HTTPURL 连接。下面的代码可以帮助您检查 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();