0

I am trying to open a PDF file with Chrome customTab. However, I know that the PDF becomes available only during certain time periods and returns a 404 error at other times. 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.

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?

Prejith P
  • 195
  • 3
  • 12

2 Answers2

3

Before launch the PDF Url first you need to call HTTPURL Connection for the status. Below code helps you to check the status of the PDF URL.

Once you get success code 200 then and then call 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();
Chirag
  • 56,621
  • 29
  • 151
  • 198
  • Didn't think of that. Do you think there would be some way to implement this with customTabs though, as this implementation requires connecting twice and hence, could use up a lot of data? – Prejith P Jan 01 '18 at 08:38
  • Is the file being downloaded from a domain you own? If that's the case, then you can redirect back to your app, using a custom URL scheme or use the PostMessageService to send a message to the app. If you do not own the domain, instead of doing a GET request, try doing a HEAD request. It should 404 as well, but will not download the whole file. – andreban Jan 02 '18 at 08:49
1

just use some library for getting status code before open your customChromeTab.

by doing this you can check for 4o4.

hope this answer will help you.

Prashant Jajal
  • 3,469
  • 5
  • 24
  • 38