1

I'm using loopj to post some data. It all works fine but I have some problems:

  • What is a good way to handle the case where no internet connection is available? At the moment I just retry after some time but like this, I have to store the data locally because if the user closes the app or even shuts down his device, the data won't be sent. Maybe it's the only way or do you know another way to do it?

  • When I post some data and the connection is bad, it sometimes just stopps. Does loopj have some kind of a timeout? How can you change that? And how can you know that it wasn't submitted?

I just need an extremely stable way to post data. Do you recommend to use another library than loopj?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
DominicM
  • 2,186
  • 5
  • 24
  • 42

2 Answers2

1

A way to reliably transfer data:

  1. Insert request in SQLite database
  2. Make POST request
  3. Delete appropriate row onSuccess()
  4. Retry pending rows on SQLite when internet is back (android.net.conn.CONNECTIVITY_CHANGE)

From AsyncHttpClient class:

private static final int DEFAULT_MAX_CONNECTIONS = 10;
private static final int DEFAULT_SOCKET_TIMEOUT = 10 * 1000;
private static final int DEFAULT_MAX_RETRIES = 5;
private static final int DEFAULT_SOCKET_BUFFER_SIZE = 8192;
Sangharsh
  • 2,999
  • 2
  • 15
  • 27
0

You can override onFailure() when it failed, and deal with the situation there.

@Override
public void onFailure(Throwable e) {
    Log.d(TAG, e.toString());
}

And yes, there is timeout in AsyncHttpClient, 10 seconds by default. You can change it by setTimeout(int).

iForests
  • 6,757
  • 10
  • 42
  • 75