1

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 !

  • The problem is in getting the zip file from the site. Writing is ok. When I try to get the content type from URL, I get 'text/html'. Also If you open the downloaded file in notepad, you will get a html page with all html code. Please investigate in the website, hopefully you will be able to resolve. Good luck. – Saqib Rezwan Mar 14 '18 at 05:53
  • In the website, is download content type, attachment is handled properly ? – Saqib Rezwan Mar 14 '18 at 05:54
  • Ok thank you @SaqibRezwan for your response, let me check and i am going to return back to you soon ! – Nathan4Yehoshua Mar 15 '18 at 23:59
  • Hi @SaqibRezwan, you were right about the content-type, after investigations it happens that the websites firstly return an authentication page which saves cookies, and then the browser redirect to a new url location...all this happens transparently with a browser. Now my challenge is to mimic this behavior in an android app ! I have tried what i found in the following [link](https://stackoverflow.com/questions/16150089/how-to-handle-cookies-in-httpurlconnection-using-cookiemanager) without success...content-type does'nt change...I am still searching, but if any one as a clue, i take! Thanks – Nathan4Yehoshua Mar 19 '18 at 01:31
  • I am glad that, you found the problem and I could be any any kind of help. Good luck. :) – Saqib Rezwan Mar 20 '18 at 06:02

1 Answers1

0

The problem is in getting the zip file from the site. Writing part in android is alright. When I tried to get the content type from URL, I get content type of 'text/html'. when I opened the downloaded file (.zip file) in notepad, I got a html page with all html code. The problem is from the website where the file is being written. In the website, the content-type should be properly maintained.

Saqib Rezwan
  • 1,382
  • 14
  • 20