简体   繁体   English

使用Chrome customTabs时如何捕捉错误

[英]How to catch errors when using Chrome customTabs

I am trying to open a PDF file with Chrome customTab. 我正在尝试使用Chrome customTab打开PDF文件。 However, I know that the PDF becomes available only during certain time periods and returns a 404 error at other times. 但是,我知道PDF仅在某些时间段内可用,而在其他时间返回404错误。 So I started looking for some way to handle this 404 error being returned but I don't know where I can retrieve the HTML status code from. 因此,我开始寻找某种方法来处理返回的404错误,但是我不知道从哪里可以检索HTML状态代码。

This is my code: 这是我的代码:

     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: I want to manage a 404 error when opening chrome customTabs, how can I do this? tl; dr:打开chrome customTabs时我想管理404错误,该怎么办?

Before launch the PDF Url first you need to call HTTPURL Connection for the status. 首先启动PDF Url之前,您需要调用HTTPURL Connection来获取状态。 Below code helps you to check the status of the PDF URL. 以下代码可帮助您检查PDF URL的状态。

Once you get success code 200 then and then call CustomIntentTab. 一旦获得成功代码200,然后再调用CustomIntentTab。

Below code helps you to check the status code. 以下代码可帮助您检查状态代码。

URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();

int code = connection.getResponseCode();

just use some library for getting status code before open your customChromeTab . 在打开customChromeTab之前,只需使用一些library来获取status code customChromeTab

by doing this you can check for 4o4. 通过这样做,您可以检查4o4。

hope this answer will help you. 希望这个答案对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM