重试HTTP(S)POST,直到他们在Android上成功 [英] Retry HTTP(S) POSTs until they succeed on Android
问题描述
我有一些数据将通过http(s)从Android应用程序发送到服务器。它需要按顺序发送。
I have some data which will be sent from an Android app to a server via http(s). It needs to be sent in-order.
是否已经存在一种排队http请求(对于同一服务器)并重试它们直到它们完成的方式(不一定成功) )?
Does there already exist a way of queuing http requests (for the same server) and retrying them until they complete (not necessarily succeed)?
我的问题是,如果没有网络覆盖,http请求可能会失败。应该有某种形式的指数退避,或者一个监听器(用于网络重新连接)来提示重试队列的头部。
My problem is that http requests may fail if there is not network coverage. There should be some form of exponential back-off, or a listener (for network reconnection) to prompt retrying the head of the queue.
我可以自己写这个,但是我想检查一下我是不是在重新发明轮子。
I can write this myself, but I want to check that I'm not re-inventing the wheel.
推荐答案
有几个选项可以做到:
排球:
RequestQueue queue = Volley.newRequestQueue(ctx); // ctx is the context
StringRequest req = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String data) {
// We handle the response
}
},
new Response.ErrorListener() {
@Override
// handle response
}
);
queue.add(req);
OkHttp :
OkHttpClient client = new OkHttpClient();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
// Handle error
}
@Override
public void onResponse(Response response) throws IOException {
//handle response
}
});
或者您可以在请求中使用计数器并让服务器对它们进行排序。
如果您有兴趣了解有关Android Http库的更多详细信息,我会立即写一篇文章。看看这里
Or you can use a counter in your requests and let the server order them. If you are interested to have more details about Android Http libraries i wrote a post recentely. Give a look here
这篇关于重试HTTP(S)POST,直到他们在Android上成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!