first of all sorry for my bad English and thanks for reading.

I'm developing Android App which have a WebView and I want to download files from aspx website, but I cannot. I'm getting this error:

06-09 10:41:50.297 19639-19741/my.package.name W/System.err: java.io.FileNotFoundException: http://URL/FILE_NAME.pdf
06-09 10:41:50.297 19639-19741/my.package.name W/System.err:     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
06-09 10:41:50.305 19639-19741/my.package.name W/System.err:     at consum.damal.portaltiendas.AsyncTaskDescarga.doInBackground(AsyncTaskDescarga.java:84)
06-09 10:41:50.305 19639-19741/my.package.name W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-09 10:41:50.305 19639-19741/my.package.name W/System.err:     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-09 10:41:50.305 19639-19741/my.package.name W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-09 10:41:50.305 19639-19741/my.package.name W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-09 10:41:50.305 19639-19741/my.package.name W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-09 10:41:50.305 19639-19741/my.package.name W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-09 10:41:50.305 19639-19741/my.package.name W/System.err:     at java.lang.Thread.run(Thread.java:856)

I'm getting the Response Code from the HttpURLConnection and it returns 400.

If I try to download this file from Chrome it download Ok, but not on my own WebView.

I'm using an AsyncTask to download it, this is my doInBackground()

@Override
 protected Object doInBackground(Object[] params) {

String filename = URLUtil.guessFileName(params[0].toString(), params[1].toString(), params[2].toString());

        HttpURLConnection c;
        URL url;

        try {

            url = new URL(params[0].toString());

            c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setRequestProperty("Authorization Credentials", "user:pass");

            CookieManager cookieManager = new CookieManager();
            CookieHandler.setDefault(cookieManager);

            c.connect();

        } catch (IOException e1) {
            e1.printStackTrace();
        }

        File myFilesDir = new File("/myDownloadDir");

        File file = new File(myFilesDir, filename);

        if (file.exists()) {
            file.delete();
        }

        if ((myFilesDir.mkdirs() || myFilesDir.isDirectory())) {
            try {

                Log.d("RESPONSE CODE",""+c.getResponseCode());

                InputStream is = c.getInputStream();

                FileOutputStream fos = new FileOutputStream(myFilesDir
                        + "/" + filename);

                byte[] buffer = new byte[c.getContentLength()];
                int len1 = 0;
                while ((len1 = is.read(buffer)) > 0) {
                    fos.write(buffer, 0, len1);
                }

                fos.close();
                is.close();

            } catch (Exception e) {
                e.printStackTrace();
            }

            if (file.exists()) {
                return "File downloaded!";
            } else {
                Log.e("file.exists()", "file not found");
            }
        } else {
            Log.e("myFilesDir.mkdirs() || myFilesDir.isDirectory()", "unable to create folder");
        }

}

I have an alternative download on website and the URL contains:

/download.aspx?SourceUrl=FILE_URL

Can I redirect this URL to the file source path?

And if I can, How could I do the redirection to the file source path?

Thanks for help!!

Related posts

Recent Viewed