I'm trying to programatically download a file from an api which the url has been provided, but the downloaded file always ended up incomplete, with a size lesser than the file I manually downloaded with the url...
This is my code :
try{
URL url = new URL(filesToBeDwlUrl);
HttpURLConnection c = (HttpURLConnection)url.openConnection();
c.setRequestMethod("GET");
c.connect();
//if connection response is not OK then log
if(c.getResponseCode() != HttpURLConnection.HTTP_OK){
Log.e("Response", "Server returned HTTP "+ c.getResponseCode());
}
storagePath = new File(mContext.getFilesDir().getAbsoluteFile() + "/"+ "AudiosArchiveFolder");
if(!storagePath.isDirectory())
storagePath.mkdir();
Log.v("Download-info ", "Directory created in internal storage : "+ storagePath);
outputZipFile = new File( storagePath, fileName);
FileOutputStream fos = new FileOutputStream(outputZipFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1;
while((len1 = is.read(buffer)) != -1){
fos.write(buffer, 0, len1);
}
fos.flush();
fos.close();
is.close();
}catch (Exception e){
mProgressDialog.dismiss();
outputZipFile = null;
Toast.makeText(mContext, "An error occurred. Multimedia content could not be saved on your device. Please contact customer support", Toast.LENGTH_SHORT).show();
Log.e("Download-error", "Error found: "+e.getMessage());
}
The url filesToBeDwlUrl value is : "http://play.digtransformations.com/api.custom.action.execute/YUBhLmFh/YWE=/81GQ/zbfd874/O8npdvQbJmexu8Hq/j8Hs";
I cannot yet figure out if the problem is with my code or if it is with the priovided url, since i can obtain the right zipped file when downloading manually the file.
Thanks for any help !