MikkoP
MikkoP

Reputation: 5092

How to retry upload to server in Android

I have an application that receives text messages with a BroadcastReceiver. It tries to upload them to a server and if it fails it has to retry.

I wonder which would be a good way to retry uploading. Should I use some kind of timer that fires an event like a BroadcastReceiver to retry sending messages every so often (how?) or something else?

Upvotes: 0

Views: 858

Answers (3)

Zhang Xiang
Zhang Xiang

Reputation: 432

you can try to use JobScheduler to schedule your upload task.

if the task fails, just calls jobFinished(mJobParam, needsReschedule),your task will be rescheduled

if your want to set the retry policy or the retry interval to wait when the task fails,you can set setBackoffCriteria(long initialBackoffMillis, int backoffPolicy)

AndroidTaskScheduling docs

i hope this can help you.

Upvotes: 0

Mauker
Mauker

Reputation: 11497

You could use the Android AlarmManager system. You can schedule a service to keep trying to upload your data in case you get a timeout, like d.i.v.a said, or any other IO problem in the first try.

Please refer to: https://developer.android.com/training/scheduling/alarms.html to learn a bit more about alarms.

Another useful link:

If you don't want to use the system alarm for some reason, you can also try to use Thread.sleep() inside a "retry loop", assuming you're using a service, AsyncTask, etc. (Don't call Thread.sleep in the main thread!)

Upvotes: 1

KOTIOS
KOTIOS

Reputation: 11194

It's worth if u get the timeout and hit the upload again if extends this time out..

DefaultHttpClient h;
// ...
Log.d(TAG, "http.socket.timeout: " +
      h.getParams().getParameter("http.socket.timeout"));
Log.d(TAG, "http.connection.timeout: "
      + h.getParams().getParameter("http.connection.timeout"));

Upvotes: 0

Related Questions