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
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...
Related
I am downloading HTML file from server using URLConnection but I am getting FileNotFoundException. My path is correct. Using same code same file is downloaded on android of API level 25 but on API level less than it is not downloading. Also connection.getContentLength(); is giving 311 value always. I am not getting any answer to solve this. I am hereby adding my code.
My class file code is
String HomeScreenResourcefilename = WebHomescreenResObj.getString("FileName");
int ProductTargetID = WebHomescreenResObj.getInt("ProductTargetID");
String WebURL_Part1 = context.getResources().getString(R.string.FileDownloadRootPath)+"/ExhibitorData/";
String WebURL_Part2 = GlobalVariables.tradeShowName+" - "+GlobalVariables.exhibitorName+"/HTMLHomeScreen/"+ProductTargetID +"/"+HomeScreenResourcefilename;
String WebURL = WebURL_Part1 + WebURL_Part2;
try {
URL url = new URL(WebURL);
File file = new File(context.getFilesDir(), HomeScreenResourcefilename);
URLConnection connection = url.openConnection();
connection.connect();
FileOutputStream fileOutput = new FileOutputStream(file);
//Stream used for reading the data from the internet
InputStream inputStream = connection.getInputStream();
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
}
fileOutput.close();
} catch (Exception e1) {
e1.printStackTrace();
LogE("error in 14 :"+e1);
}
Use HttpURLConnection and then connection.getResponseCode() to get the status code. If it is greater than 400, that might be the reason.
Update: Use url encoding.
i have this url http://app.oviewz.com/proceso_seleccions/110.png?style=medium&token=q9W0BQpU. when u put on any web browser automatically download the file
but when i try to download by using this android code:
String urllogo="http://app.oviewz.com/proceso_seleccions/110.png?style=medium&token=q9W0BQpU";
URL url = new URL(urllogo);
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
// <span id="IL_AD2" class="IL_AD">connect</span>
urlConnection.connect();
// set the path where we want to save the file
File SDCardRoot = Environment.getExternalStorageDirectory();
// create a new file, to save the <span id="IL_AD7"
// class="IL_AD">downloaded</span> file
File file = new File(SDCardRoot, "logo.jpg");
FileOutputStream fileOutput = new FileOutputStream(file);
// Stream used for reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
// this is the total size of the file which we are <span
// id="IL_AD12" class="IL_AD">downloading</span>
// create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
}
// close the output stream when complete //
fileOutput.close();
is raised FileNotFoundException. anyone can help me what i'm doing wrong?
When I open this URL in my browser, it redirects me to http://app.oviewz.com/. This means that the image is not available publicly i.e. You need to be logged in to view the image. So, you can't directly download the image using URL. If the website has some API for login, you can use that to get required access token and send the same in header of this request, the way they do in facebook or g+ API.
URL url = new URL(path);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = "/mnt/sdcard/Android/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "version.txt");
if(outputFile.exists()){
outputFile.delete();
}
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
Exception caught: java.io.FileNotFoundException http://192.168.2.143/version.txt, where path="http://192.168.2.143/version.txt", I use browser in my device to open http://192.168.2.143/version.txt, can be opened. I have INTERNET permission in my manifest. Any idea?
It's the same problem I was having:
HttpUrlConnection returns FileNotFoundException if you try to read the getInputStream() from the connection.
You should instead use getErrorStream() when the status code is higher than 400.
More than this, please be careful since it's not only 200 to be the success status code, even 201, 204, etc. are often used as success statuses.
Here is an example of how I went to manage it
... connection code code code ...
// Get the response code
int statusCode = connection.getResponseCode();
InputStream is = null;
if (statusCode >= 200 && statusCode < 400) {
// Create an InputStream in order to extract the response object
is = connection.getInputStream();
}
else {
is = connection.getErrorStream();
}
... callback/response to your handler....
In this way, you'll be able to get the needed response in both success and error cases.
Hope this helps!
Try this to read file over the internet: Reading Text File From Server on Android
I'm not sure you can use File.
I have an app to download zip file content from drop box (which is publicly shared path). i wrote download code using HttpURLConnection but its not working as intended and instead is downloading a small portion (after download zip file showing 31 kb but its original size is 3mb). i am attching my code. please help me to solve this.
URL url = new URL("drop box public share url");
//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setAllowUserInteraction(false);
urlConnection.setInstanceFollowRedirects(true);
urlConnection.setConnectTimeout(5 * 1000);
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
File SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(SDCardRoot,"/download/sample.zip");
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
int totalSize = urlConnection.getContentLength();
int downloadedSize = 0;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
onProgressUpdate(downloadedSize, totalSize);
}
//close the output stream when done
fileOutput.close();
inputStream.close();
It seems that the method call:
setDoOuput(true);
makes the request a POST (see
What exactly does URLConnection.setDoOutput() affect?)
Removing it seems to fix the issue.
Try to use plain URLConnection not HttpURLConnection. And see the output.
I am trying to download a zipped file from the web on the first instance of the application; can someone point me to a tutorial for this (I don't see good documentation on developer.android)? I understand how to check if it's the initial start or not, and also how to use java.util.zip once I get the file, but it's the in-between where I'm lost.
I think your question is about how to download the file. So, to download a file, use code similar to the following:
URL u = new URL("http://www.example.com/downloadfile.zip");
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
FileOutputStream fNew = new FileOutputStream(new File(root,"downloadfile.zip"));
InputStream inStream = c.getInputStream();
byte[] buffer = new byte[1024];
int inlen = 0;
while ( (inlen = inStream.read(buffer)) > 0 ) {
fNew.write(buffer,0, inlen);
}
fNew.close();
inStream.close();
Of course, wrap with appropriate error checking