1

I am using the following code to download PDF files.

private class downloadPaper extends AsyncTask<String, String, String> {
    private ProgressDialog progressDialog;
    private boolean isDownloaded;
    private String destination = *path to folder on phone*;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        this.progressDialog = new ProgressDialog(ActivityPaper.this);
        this.progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        this.progressDialog.setCancelable(false);
        this.progressDialog.show();
    }
    @Override
    protected String doInBackground(String... url) {
        int count;
        try {
            URL getUrl = new URL(url[0]);
            URLConnection connection = getUrl.openConnection();
            connection.connect();
            // getting file length
            int lengthOfFile = connection.getContentLength();
            // input stream to read file - with 8k buffer
            InputStream input = new BufferedInputStream(getUrl.openStream(), 8192);
            // Output stream to write file
            OutputStream output = new FileOutputStream(destination);
            byte data[] = new byte[1024];
            long total = 0;
            while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                // After this onProgressUpdate will be called
                publishProgress("" + (int) ((total * 100) / lengthOfFile));
                Log.d(TAG, "Progress: " + (int) ((total * 100) / lengthOfFile));
                // writing data to file
                output.write(data, 0, count);
            }
            // flushing output
            output.flush();
            // closing streams
            output.close();
            input.close();
            return "success";
        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }
        return getString(R.string.somethingWrong);
    }
    /**
     * Updating progress bar
     */
    protected void onProgressUpdate(String... progress) {
        // setting progress percentage
        progressDialog.setProgress(Integer.parseInt(progress[0]));
    }
    @Override
    protected void onPostExecute(String message) {
        // dismiss the dialog after the file was downloaded
        this.progressDialog.dismiss();
        // Display File path after downloading
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
    }
}

I am getting the following response,

E/Error:: Unexpected status line: <html>

I have tried changing the file source URL to other PDFs on different websites and its working fine. The error only shows when I try to download from my server. I am guessing its a server configuration issue.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • refer this link https://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog?answertab=votes#tab-top –  Nov 15 '18 at 12:30
  • So it seems you caught an exception. Can you print more of the exception (e.g. stack trace) to have more information what is going wrong and where? – Henry Nov 15 '18 at 12:34
  • I suggest you to check the link you provide to download. I am pretty sure it is returning some html doc or so. Like you say, if you download file from other url then it's working fine. – Sanjog Shrestha Nov 15 '18 at 12:43
  • it's certainly looking about the user agent or else to not let bots scrap their pdfs. Look at your complete request and try to reproduce with curl or whatever. – mcfly Nov 15 '18 at 13:35

0 Answers0