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.