@@ -38,118 +38,88 @@ class AsyncHttpRequest implements Runnable {
38
38
private final HttpContext context ;
39
39
private final HttpUriRequest request ;
40
40
private final AsyncHttpResponseHandler responseHandler ;
41
- private boolean isBinaryRequest ;
42
41
private int executionCount ;
43
42
44
43
public AsyncHttpRequest (AbstractHttpClient client , HttpContext context , HttpUriRequest request , AsyncHttpResponseHandler responseHandler ) {
45
44
this .client = client ;
46
45
this .context = context ;
47
46
this .request = request ;
48
47
this .responseHandler = responseHandler ;
49
- if (responseHandler instanceof BinaryHttpResponseHandler ) {
50
- this .isBinaryRequest = true ;
51
- }
52
48
}
53
49
54
50
@ Override
55
51
public void run () {
56
- try {
57
- if (responseHandler != null ) {
58
- responseHandler .sendStartMessage ();
59
- }
52
+ if (responseHandler != null ) {
53
+ responseHandler .sendStartMessage ();
54
+ }
60
55
56
+ try {
61
57
makeRequestWithRetries ();
62
-
63
- if (responseHandler != null ) {
64
- responseHandler .sendFinishMessage ();
65
- }
66
58
} catch (IOException e ) {
67
59
if (responseHandler != null ) {
68
- responseHandler .sendFinishMessage ();
69
- if (this .isBinaryRequest ) {
70
- responseHandler .sendFailureMessage (e , (byte []) null );
71
- } else {
72
- responseHandler .sendFailureMessage (e , (String ) null );
73
- }
60
+ responseHandler .sendFailureMessage (0 , null , (byte []) null , e );
74
61
}
75
62
}
63
+
64
+ if (responseHandler != null ) {
65
+ responseHandler .sendFinishMessage ();
66
+ }
76
67
}
77
68
78
- private void makeRequest () throws IOException , InterruptedException {
69
+ private void makeRequest () throws IOException {
79
70
if (!Thread .currentThread ().isInterrupted ()) {
80
- try {
81
- // Fixes #115
82
- if (request .getURI ().getScheme () == null )
83
- throw new MalformedURLException ("No valid URI scheme was provided" );
84
- HttpResponse response = client .execute (request , context );
85
- if (!Thread .currentThread ().isInterrupted ()) {
86
- if (responseHandler != null ) {
87
- responseHandler .sendResponseMessage (response );
88
- }
89
- } else {
90
- throw new InterruptedException ("makeRequest was interrupted" );
91
- }
92
- } catch (IOException e ) {
93
- if (!Thread .currentThread ().isInterrupted ()) {
94
- throw e ;
71
+ // Fixes #115
72
+ if (request .getURI ().getScheme () == null ) {
73
+ // subclass of IOException so processed in the caller
74
+ throw new MalformedURLException ("No valid URI scheme was provided" );
75
+ }
76
+
77
+ HttpResponse response = client .execute (request , context );
78
+
79
+ if (!Thread .currentThread ().isInterrupted ()) {
80
+ if (responseHandler != null ) {
81
+ responseHandler .sendResponseMessage (response );
95
82
}
96
83
}
97
84
}
98
85
}
99
86
100
- private void makeRequestWithRetries () throws ConnectException {
101
- // This is an additional layer of retry logic lifted from droid-fu
102
- // See: https://github.com/kaeppler/droid-fu/blob/master/src/main/java/com/github/droidfu/http/BetterHttpRequestBase.java
87
+ private void makeRequestWithRetries () throws IOException {
103
88
boolean retry = true ;
104
89
IOException cause = null ;
105
90
HttpRequestRetryHandler retryHandler = client .getHttpRequestRetryHandler ();
106
- while (retry ) {
107
- try {
108
- makeRequest ();
109
- return ;
110
- } catch (ClientProtocolException e ) {
111
- if (responseHandler != null ) {
112
- responseHandler .sendFailureMessage (e , "cannot repeat the request" );
113
- }
114
- return ;
115
- } catch (UnknownHostException e ) {
116
- if (responseHandler != null ) {
117
- responseHandler .sendFailureMessage (e , "can't resolve host" );
91
+ try
92
+ {
93
+ while (retry ) {
94
+ try {
95
+ makeRequest ();
96
+ return ;
97
+ } catch (UnknownHostException e ) {
98
+ // switching between WI-FI and mobile data networks can cause a retry which then results in an UnknownHostException
99
+ // while the WI-FI is initialising. The retry logic will be invoked here, if this is NOT the first retry
100
+ // (to assist in genuine cases of unknown host) which seems better than outright failure
101
+ cause = new IOException ("UnknownHostException exception: " + e .getMessage ());
102
+ retry = (executionCount > 0 ) && retryHandler .retryRequest (cause , ++executionCount , context );
103
+ } catch (NullPointerException e ) {
104
+ // there's a bug in HttpClient 4.0.x that on some occasions causes
105
+ // DefaultRequestExecutor to throw an NPE, see
106
+ // http://code.google.com/p/android/issues/detail?id=5255
107
+ cause = new IOException ("NPE in HttpClient: " + e .getMessage ());
108
+ retry = retryHandler .retryRequest (cause , ++executionCount , context );
109
+ } catch (IOException e ) {
110
+ cause = e ;
111
+ retry = retryHandler .retryRequest (cause , ++executionCount , context );
118
112
}
119
- return ;
120
- } catch (ConnectTimeoutException e ) {
121
- if (responseHandler != null ) {
122
- responseHandler .sendFailureMessage (e , "connection timed out" );
123
- }
124
- } catch (SocketException e ) {
125
- // Added to detect host unreachable
126
- if (responseHandler != null ) {
127
- responseHandler .sendFailureMessage (e , "can't resolve host" );
113
+ if (retry && (responseHandler != null )) {
114
+ responseHandler .sendRetryMessage ();
128
115
}
129
- return ;
130
- } catch (SocketTimeoutException e ) {
131
- if (responseHandler != null ) {
132
- responseHandler .sendFailureMessage (e , "socket time out" );
133
- }
134
- return ;
135
- } catch (IOException e ) {
136
- cause = e ;
137
- retry = retryHandler .retryRequest (cause , ++executionCount , context );
138
- } catch (NullPointerException e ) {
139
- // there's a bug in HttpClient 4.0.x that on some occasions causes
140
- // DefaultRequestExecutor to throw an NPE, see
141
- // http://code.google.com/p/android/issues/detail?id=5255
142
- cause = new IOException ("NPE in HttpClient" + e .getMessage ());
143
- retry = retryHandler .retryRequest (cause , ++executionCount , context );
144
- } catch (InterruptedException e ) {
145
- cause = new IOException ("Request was interrupted while executing" );
146
- retry = retryHandler .retryRequest (cause , ++executionCount , context );
147
116
}
117
+ } catch (Exception e ) {
118
+ // catch anything else to ensure failure message is propagated
119
+ cause = new IOException ("Unhandled exception: " + e .getMessage ());
148
120
}
149
-
150
- // no retries left, crap out with exception
151
- ConnectException ex = new ConnectException ();
152
- ex .initCause (cause );
153
- throw ex ;
121
+
122
+ // cleaned up to throw IOException
123
+ throw (cause );
154
124
}
155
125
}
0 commit comments