0

I have a published Android application that has an HTTP audio download process.

This processed worked fine until today.

whats wrong with my code?

final URL downloadFileUrl = new URL(mPerformanceSong.getPreviewUrl());
final HttpURLConnection httpURLConnection = (HttpURLConnection) downloadFileUrl.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoOutput(true);
httpURLConnection.setConnectTimeout(10000);
httpURLConnection.setReadTimeout(10000);
httpURLConnection.connect();

mTrackDownloadFile = new File(RecordPerformance.this.getCacheDir(), "mediafile");
mTrackDownloadFile.createNewFile();
final FileOutputStream fileOutputStream = new FileOutputStream(mTrackDownloadFile);
final byte buffer[] = new byte[16 * 1024];

final InputStream inputStream = httpURLConnection.getInputStream();

int len1 = 0;
while ((len1 = inputStream.read(buffer)) > 0) {
        fileOutputStream.write(buffer, 0, len1);
}

fileOutputStream.flush();
fileOutputStream.close();

The content of the downloaded file appears to be gzip.

does this mean i need to wrap my inputStream in GZIPInputStream?

an example download URL is

http://www.amazon.com/gp/dmusic/get_sample_url.html?ASIN=B008TMSNMI
Hector
  • 4,016
  • 21
  • 112
  • 211

1 Answers1

1

I'm actually surprised it's downloading anything - are you sure it is?

The http url that you've posted:

http://www.amazon.com/gp/dmusic/get_sample_url.html?ASIN=B008TMSNMI

Is currently redirecting to an https cloudfront address:

https://d28julafmv4ekl.cloudfront.net/...

HttpUrlConnection will not follow redirects across schemes (ie from http to https).

So if you change your *.amazon.com URLs to https, perhaps it would fix your issue...

Sam Dozor
  • 40,335
  • 6
  • 42
  • 42
  • i am ABSOLUTELY sure that it used to work. and now it doesnt. – Hector Sep 26 '14 at 19:02
  • 1
    are you absolutely sure it used to redirect to a different scheme? – Sam Dozor Sep 26 '14 at 19:03
  • no i am not sure of what was happening, i have had no need to investigate this until now, it was working and i was happy:). Now that i have change http: to https: in the original URL and set httpURLConnection.setRequestProperty("Accept-Encoding", "gzip, deflate"); and httpURLConnection.setInstanceFollowRedirects(true); its started working. Thanks very much for your help – Hector Sep 26 '14 at 19:07
  • 1
    well, as a test, i would try an https url. or a url that you know does not redirect. – Sam Dozor Sep 26 '14 at 19:08