I am using a AsyncTask to download a file from a URL in Android.
This is the class to download the file in the background:
//-----------------------------ASYNC DOWNLOADER--------------------------------
/**
* Background Async Task to download file
* */
class DownloadFileFromURL extends AsyncTask<String, String, String> {
/**
* Before starting background thread
* Show Progress Bar Dialog
* */
@SuppressWarnings("deprecation")
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
//TODO Zustzinfos für alle Methoden hinzufügen
/**
* This method is called for executing the background task in the AsyncTask.
* For this tutorial we are only sleeping the thread for the number of
* seconds passed as parameter of the function.
*
* @param numSeconds: life of the task
* @return the result of the background task
*/
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
OutputStream output = new FileOutputStream("/sdcard/"+Name+".xml");
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)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("ASYNC",e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
* **/
@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(String error) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
String xmlPath = Environment.getExternalStorageDirectory().toString() + "/"+Name+".xml";
Log.d("XMLDOWNLOADPATH", xmlPath);
Log.d("DOWNLOADXML","End Download XML file");
}
}
I want to be able to recognize in my main activity (the async class is a inner class of the main acticity) if there was a exception and show the error message in a Dialog or Toast.
I tried to return the value from doInBackground() to onPostExecute() and write this String to a global String variable like this:
protected String doInBackground(String... f_url) {
String error = null;
try {
}
catch(Exception e){
error = e.toString();
}
return error;
@Override
protected void onPostExecute(String error) {
globalStringvariable = error;
}
But this does not work properly, the dialog does not always show the exception message. Is this the best way to realize my problem?
That’s what you’re looking for.
Also note that if your getting
null, it can be because you are catchingExceptionas a generic, you should always try to catch a specificExceptionin order to get the message back.