-1

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);
                        }
  • Can you try changing File file = new File(context.getFilesDir(), HomeScreenResourcefilename); to File file = new File(context.getFilesDir().getAbsolutePath(), HomeScreenResourcefilename); – Kapil G Jul 31 '17 at 12:53
  • Please tell at what line the exception throws. – Ircover Jul 31 '17 at 13:06
  • InputStream inputStream = connection.getInputStream(); is throwing Exception. Also using same code another file from same server with different location is downloaded successfully. – Snehal Gurav Jul 31 '17 at 13:42
  • getAbsolutePath() is also not working. Same exception is there. Also tell me about that 311 content size. Whysame size always which throws exception. – Snehal Gurav Jul 31 '17 at 13:49

1 Answers1

0

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.

graypacket
  • 98
  • 1
  • 11