Anggrayudi H
Anggrayudi H

Reputation: 15165

Checking the content from an URL: Is it a file or webpage?

I have an app which need different action based on the content from an URL. If the content is a file, I need to download it. But, if the content is a webpage, I need to open it. As far I know, there are two URL types:

And the example for webpage: https://stackoverflow.com/questions/xxxxxx/how-to-check-a-url-contain

I only have the following code to parse a String to URL:

String requestUrl = "https://dl-ssl.google.com/android/repository/android-14_r04.zip";
URL url = new URL(requestUrl);

So, my questions:

Thanks in advance.

Upvotes: 4

Views: 4653

Answers (2)

CanCoder
CanCoder

Reputation: 1150

Permit me to add my answer though this question is a year old.

Android provides a URL utility that can check many options based on URL. Try:

        URLUtil.isFileUrl(URL)

Other options includes isDataUrl, isContentUrl etc.

Thanks

Upvotes: 0

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136162

you can check content type:

    URLConnection c = url.openConnection();
    String contentType = c.getContentType();

for html - text/html; charset=utf-8; for zip - application/zip

Upvotes: 4

Related Questions