|
| 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 | +} |
0 commit comments