Downloading a file from an URL?












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.










share|improve this question

























  • refer this link stackoverflow.com/questions/3028306/…

    – Android Team
    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
















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.










share|improve this question

























  • refer this link stackoverflow.com/questions/3028306/…

    – Android Team
    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














1












1








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.










share|improve this question
















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.







java android






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 13:30









Fantômas

32.8k156390




32.8k156390










asked Nov 15 '18 at 12:22









Tresford Himanansa IITresford Himanansa II

3715




3715













  • refer this link stackoverflow.com/questions/3028306/…

    – Android Team
    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



















  • refer this link stackoverflow.com/questions/3028306/…

    – Android Team
    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

















refer this link stackoverflow.com/questions/3028306/…

– Android Team
Nov 15 '18 at 12:30





refer this link stackoverflow.com/questions/3028306/…

– Android Team
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





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





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





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






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319430%2fdownloading-a-file-from-an-url%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319430%2fdownloading-a-file-from-an-url%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values