Skip to content

Commit 3a65f32

Browse files
author
Noor Dawod
committed
Retry request sample.
1 parent 75a5361 commit 3a65f32

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Android Asynchronous Http Client Sample
3+
Copyright (c) 2014 Marek Sebera <[email protected]>
4+
http://loopj.com
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package com.loopj.android.http.sample;
20+
21+
import android.os.Bundle;
22+
import android.widget.Toast;
23+
24+
import com.loopj.android.http.AsyncHttpClient;
25+
import java.io.IOException;
26+
import java.net.SocketTimeoutException;
27+
import java.net.UnknownHostException;
28+
29+
import org.apache.http.conn.ConnectTimeoutException;
30+
import org.apache.http.conn.ConnectionPoolTimeoutException;
31+
32+
public class RetryRequestSample extends GetSample {
33+
34+
private static boolean wasToastShown;
35+
36+
@Override
37+
protected void onCreate(Bundle savedInstanceState) {
38+
super.onCreate(savedInstanceState);
39+
40+
// The following exceptions will be whitelisted, i.e.: When an exception
41+
// of this type is raised, the request will be retried.
42+
AsyncHttpClient.allowRetryExceptionClass(IOException.class);
43+
AsyncHttpClient.allowRetryExceptionClass(SocketTimeoutException.class);
44+
AsyncHttpClient.allowRetryExceptionClass(ConnectTimeoutException.class);
45+
46+
// The following exceptions will be blacklisted, i.e.: When an exception
47+
// of this type is raised, the request will not be retried and it will
48+
// fail immediately.
49+
AsyncHttpClient.blockRetryExceptionClass(UnknownHostException.class);
50+
AsyncHttpClient.blockRetryExceptionClass(ConnectionPoolTimeoutException.class);
51+
}
52+
53+
@Override
54+
protected void onResume() {
55+
super.onResume();
56+
57+
if(!wasToastShown) {
58+
wasToastShown = true;
59+
Toast.makeText(
60+
this,
61+
"Exceptions' whitelist and blacklist updated\nSee RetryRequestSample.java for details",
62+
Toast.LENGTH_LONG
63+
).show();
64+
}
65+
}
66+
67+
@Override
68+
public String getDefaultURL() {
69+
return "http://httpbin.org/ip";
70+
}
71+
72+
@Override
73+
public int getSampleTitle() {
74+
return R.string.title_retry_handler;
75+
}
76+
}

sample/src/main/java/com/loopj/android/http/sample/WaypointsActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public class WaypointsActivity extends ListActivity {
4848
new SampleConfig(R.string.title_intent_service_sample, IntentServiceSample.class),
4949
new SampleConfig(R.string.title_post_files, FilesSample.class),
5050
new SampleConfig(R.string.title_persistent_cookies, PersistentCookiesSample.class),
51-
new SampleConfig(R.string.title_custom_ca, CustomCASample.class)
51+
new SampleConfig(R.string.title_custom_ca, CustomCASample.class),
52+
new SampleConfig(R.string.title_retry_handler, RetryRequestSample.class)
5253
};
5354

5455
@Override

sample/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@
2626
<string name="title_redirect_302">302 Redirect handling</string>
2727
<string name="title_persistent_cookies">Handling persistent cookies</string>
2828
<string name="title_custom_ca">Custom CA Example</string>
29+
<string name="title_retry_handler">Retrying requests by Exception</string>
2930
</resources>

0 commit comments

Comments
 (0)