Skip to content

Commit 8f947c3

Browse files
committed
Add retry support.
1 parent 47c0301 commit 8f947c3

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

src/com/androidquery/callback/AbstractAjaxCallback.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ public abstract class AbstractAjaxCallback<T, K> implements Runnable{
150150
private HttpUriRequest request;
151151

152152
private boolean uiCallback = true;
153+
private int retry = 0;
153154

154155
@SuppressWarnings("unchecked")
155156
private K self(){
@@ -288,6 +289,11 @@ public K timeout(int timeout){
288289
return self();
289290
}
290291

292+
public K retry(int retry){
293+
this.retry = retry;
294+
return self();
295+
}
296+
291297
/**
292298
* Set the transformer that transform raw data to desired type.
293299
* If not set, default transformer will be used.
@@ -1079,7 +1085,7 @@ private void networkWork(){
10791085

10801086
try{
10811087

1082-
network();
1088+
network(retry + 1);
10831089

10841090
if(ah != null && ah.expired(this, status) && !reauth){
10851091
AQUtility.debug("reauth needed", status.getMessage());
@@ -1212,6 +1218,31 @@ private static Map<String, Object> extractParams(Uri uri){
12121218
return params;
12131219
}
12141220

1221+
//added retry logic
1222+
private void network(int attempts) throws IOException{
1223+
1224+
if(attempts <= 1){
1225+
network();
1226+
return;
1227+
}
1228+
1229+
for(int i = 0; i < attempts; i++){
1230+
1231+
try{
1232+
network();
1233+
return;
1234+
}catch(IOException e){
1235+
if(i == attempts - 1){
1236+
throw e;
1237+
}
1238+
}
1239+
1240+
1241+
1242+
}
1243+
1244+
1245+
}
12151246

12161247
private void network() throws IOException{
12171248

tests/src/com/androidquery/test/AQueryAsyncTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,4 +1673,42 @@ public void testAjaxActiveCount() {
16731673
assertNotNull(jo.opt("responseData"));
16741674

16751675
}
1676+
1677+
public void testRetryFailed() {
1678+
1679+
String url = "http://www.androidquery.com/p/retry?wait=5000";
1680+
1681+
AjaxCallback<JSONObject> cb = new AjaxCallback<JSONObject>();
1682+
cb.weakHandler(this, "jsonCb").timeout(1000);
1683+
1684+
aq.ajax(url, JSONObject.class, cb);
1685+
1686+
waitAsync();
1687+
1688+
JSONObject jo = (JSONObject) result;
1689+
1690+
assertNull(jo);
1691+
assertEquals(-101, status.getCode());
1692+
1693+
1694+
}
1695+
1696+
public void testRetryOk() {
1697+
1698+
String url = "http://www.androidquery.com/p/retry?wait=3000";
1699+
1700+
AjaxCallback<JSONObject> cb = new AjaxCallback<JSONObject>();
1701+
cb.retry(1).weakHandler(this, "jsonCb").timeout(1000);
1702+
1703+
aq.ajax(url, JSONObject.class, cb);
1704+
1705+
waitAsync();
1706+
1707+
JSONObject jo = (JSONObject) result;
1708+
1709+
assertNotNull(jo);
1710+
assertEquals(200, status.getCode());
1711+
1712+
1713+
}
16761714
}

0 commit comments

Comments
 (0)