In my app, every 60 seconds I get updates from a website. I get this updates in background using a AsyncTask
. Here is my code :
update = new Timer();
update.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
updateItems();
}
});
}
}, 30000, 60000);
where updateItems()
method is :
public void updateItems() {
System.out.println("Every 60 sec getItems");
new DownloadTask().execute();
}
public class DownloadTask extends AsyncTask<Void, Void, Void> {
protected void onPostExecute(Void... args) {
System.out.println("Download finished");
}
protected Void doInBackground(Void... args) {
try {
UpdateItems();
} catch (Exception e1) {
e1.printStackTrace();
}
return null;
}
}
In UpdateItems()
I save all the updated images on SDcard. The problem is the onPostExecute()
is not called after doInBackground()
because I don't get the message "Download finished". On this method I want to update the ViewFlipper that is flipping and like this it is changed at second call of updateitems()
. Have anyone any idea why I have this problem?
EDIT:
public void UpdateItems() throws Exception {
String t = myPrefs.getString(TIME + idPlaylist, "time");
String d = myPrefs.getString(DATE + idPlaylist, "date");
String data_download = myPrefs.getString(DATA_DOWNLOAD + idPlaylist,
"00000000");
try {
HttpClient httpclient = new DefaultHttpClient();
String link = "some link where I send the last download date";
System.out.println("trimit " + link);
HttpPost httppost = new HttpPost(link);
try {
HttpResponse response = httpclient.execute(httppost);
/* Checking response */
if (response != null) {
String responseBody = EntityUtils.toString(response
.getEntity());
System.out
.println("Rezultat de la server :" + responseBody);
String jsonString = responseBody.toString();
JSONObject jsonObject = new JSONObject(jsonString);
String status = jsonObject.getString("status");
System.out.println("Status " + status);
if (status == "true") {
System.out.println("get updates");
GetItems();
updated = true;
dataupdate = new String[jArray.length()];
data = new String[jArray.length()];
time = new String[jArray.length()];
for (int i = 0; i < jArray.length(); i++) {
System.out
.println("last update : " + lastupdate[i]);
String[] separated = lastupdate[i].split(" ");
data[i] = separated[0];
time[i] = separated[1];
dataupdate[i] = lastupdate[i].replace("-", "");
dataupdate[i] = dataupdate[i].replace(":", "");
dataupdate[i] = dataupdate[i].replace(" ", "");
System.out.println("data ultimului download "
+ data_download + "\ndata update "
+ dataupdate[i]);
if (Long.valueOf(dataupdate[i]) > Long
.valueOf(data_download)) {
System.out.println("image update");
saveImageToSDcard(url[i], i);
if (target[i].endsWith(".mp4")) {
System.out.println("video de downloadat");
try {
saveVideoToSDcard(target[i], i);
path[i] = Environment
.getExternalStorageDirectory()
+ "/BouyguesVideos"
+ idPlaylist
+ "/" + "Video" + idPhotos[i];
} catch (SocketException e) {
System.out.println("Socket timeout");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} else
path[i] = target[i];
} else
System.out.println("image not update");
}
String actual = dataupdate[0];
int indexmax = 0;
for (int k = 1; k < jArray.length(); k++) {
if (Long.valueOf(dataupdate[k]) > Long
.valueOf(actual)) {
actual = dataupdate[k];
indexmax = k;
}
}
System.out.println("max " + actual);
prefsEditor
.putString(DATE + idPlaylist, data[indexmax]);
prefsEditor
.putString(TIME + idPlaylist, time[indexmax]);
prefsEditor.putString(DATA_DOWNLOAD + idPlaylist,
actual);
prefsEditor.commit();
File f = new File(
Environment.getExternalStorageDirectory()
+ "/BouyguesImages" + idPlaylist + "/");
File[] files = f.listFiles();
System.out
.println("pe card avem " + files.length
+ " fisiere" + " iar online "
+ jArray.length());
if (files.length != jArray.length()) {
System.out.println("imagini adaugate sau sterse");
for (int k = 0; k < jArray.length(); k++) {
System.out.println("imagini pe site "
+ idPhotos[k] + namePhotos[k]);
ok = false;
for (int i = 0; i < files.length; i++) {
System.out.println("comparam "
+ files[i].getName() + " cu "
+ idPhotos[k] + namePhotos[k]);
if (files[i].getName().equals(
idPhotos[k] + namePhotos[k])) {
System.out.println("exista");
i = files.length;
ok = true;
}
}
if (ok == false) {
System.out
.println("not exist-must save now");
try {
saveImageToSDcard(url[k], k);
} catch (IOException e1) {
e1.printStackTrace();
}
if (target[k].endsWith(".mp4")) {
try {
saveVideoToSDcard(target[k], k);
path[k] = Environment
.getExternalStorageDirectory()
+ "/BouyguesVideos"
+ idPlaylist
+ "/"
+ "Video" + idPhotos[k];
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
for (int k = 0; k < files.length; k++) {
System.out.println("imagini deja salvate "
+ files[k].getName());
ok = false;
for (int i = 0; i < jArray.length(); i++) {
System.out.println("comparam "
+ files[k].getName() + " cu "
+ idPhotos[i] + namePhotos[i]);
if (files[k].getName().equals(
idPhotos[i] + namePhotos[i])) {
System.out.println("exista");
i = jArray.length();
ok = true;
}
}
if (ok == false) {
System.out.println("delete- not exist more");
files[k].delete();
}
}
} else {
System.out.println("no updates");
}
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
} catch (Exception e) {
e.printStackTrace();
}
}
You should add the @Override
over the onPostExecute and doInBackground (to be sure they're both correctly overriden).
I think it will have something to do with this.
UPDATE:
Try this:
public class DownloadTask extends AsyncTask<Void, Void, Object> {
@Override
protected Object doInBackground(Void... params) {
...
}
@Override
protected void onPostExecute(Object object) {
....
}
More details here: Can't Override onPostExecute() method in AsyncTask Class or get it to trigger