Skip to content

Commit 803a85a

Browse files
committed
Basic library refactoring
1 parent fd4d9b2 commit 803a85a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+995
-3496
lines changed

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ android {
2424
}
2525

2626
dependencies {
27-
compile 'cz.msebera.android:httpclient:4.3.6'
27+
compile 'cz.msebera.android:httpclient:4.4.1.1'
2828
}
2929

3030
android.libraryVariants.all { variant ->

library/src/main/java/com/loopj/android/http/AsyncHttpClient.java

Lines changed: 47 additions & 1500 deletions
Large diffs are not rendered by default.

library/src/main/java/com/loopj/android/http/AsyncHttpRequest.java

Lines changed: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,23 @@
1818

1919
package com.loopj.android.http;
2020

21+
import com.loopj.android.http.handlers.RangeFileAsyncHttpResponseHandler;
22+
import com.loopj.android.http.interfaces.ResponseHandlerInterface;
23+
import com.loopj.android.http.utils.Utils;
24+
2125
import java.io.IOException;
2226
import java.net.MalformedURLException;
23-
import java.net.UnknownHostException;
2427
import java.util.concurrent.atomic.AtomicBoolean;
2528

2629
import cz.msebera.android.httpclient.HttpResponse;
27-
import cz.msebera.android.httpclient.client.HttpRequestRetryHandler;
2830
import cz.msebera.android.httpclient.client.methods.HttpUriRequest;
29-
import cz.msebera.android.httpclient.impl.client.AbstractHttpClient;
30-
import cz.msebera.android.httpclient.protocol.HttpContext;
31+
import cz.msebera.android.httpclient.impl.client.CloseableHttpClient;
3132

3233
/**
3334
* Internal class, representing the HttpRequest, done in asynchronous manner
3435
*/
3536
public class AsyncHttpRequest implements Runnable {
36-
private final AbstractHttpClient client;
37-
private final HttpContext context;
37+
private final CloseableHttpClient client;
3838
private final HttpUriRequest request;
3939
private final ResponseHandlerInterface responseHandler;
4040
private final AtomicBoolean isCancelled = new AtomicBoolean();
@@ -43,9 +43,8 @@ public class AsyncHttpRequest implements Runnable {
4343
private volatile boolean isFinished;
4444
private boolean isRequestPreProcessed;
4545

46-
public AsyncHttpRequest(AbstractHttpClient client, HttpContext context, HttpUriRequest request, ResponseHandlerInterface responseHandler) {
47-
this.client = Utils.notNull(client, "client");
48-
this.context = Utils.notNull(context, "context");
46+
public AsyncHttpRequest(CloseableHttpClient httpClient, HttpUriRequest request, ResponseHandlerInterface responseHandler) {
47+
this.client = Utils.notNull(httpClient, "client");
4948
this.request = Utils.notNull(request, "request");
5049
this.responseHandler = Utils.notNull(responseHandler, "responseHandler");
5150
}
@@ -103,7 +102,7 @@ public void run() {
103102
}
104103

105104
try {
106-
makeRequestWithRetries();
105+
makeRequest();
107106
} catch (IOException e) {
108107
if (!isCancelled()) {
109108
responseHandler.sendFailureMessage(0, null, null, e);
@@ -143,7 +142,7 @@ private void makeRequest() throws IOException {
143142
((RangeFileAsyncHttpResponseHandler) responseHandler).updateRequestHeaders(request);
144143
}
145144

146-
HttpResponse response = client.execute(request, context);
145+
HttpResponse response = client.execute(request);
147146

148147
if (isCancelled()) {
149148
return;
@@ -167,49 +166,6 @@ private void makeRequest() throws IOException {
167166
responseHandler.onPostProcessResponse(responseHandler, response);
168167
}
169168

170-
private void makeRequestWithRetries() throws IOException {
171-
boolean retry = true;
172-
IOException cause = null;
173-
HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
174-
try {
175-
while (retry) {
176-
try {
177-
makeRequest();
178-
return;
179-
} catch (UnknownHostException e) {
180-
// switching between WI-FI and mobile data networks can cause a retry which then results in an UnknownHostException
181-
// while the WI-FI is initialising. The retry logic will be invoked here, if this is NOT the first retry
182-
// (to assist in genuine cases of unknown host) which seems better than outright failure
183-
cause = new IOException("UnknownHostException exception: " + e.getMessage());
184-
retry = (executionCount > 0) && retryHandler.retryRequest(e, ++executionCount, context);
185-
} catch (NullPointerException e) {
186-
// there's a bug in HttpClient 4.0.x that on some occasions causes
187-
// DefaultRequestExecutor to throw an NPE, see
188-
// https://code.google.com/p/android/issues/detail?id=5255
189-
cause = new IOException("NPE in HttpClient: " + e.getMessage());
190-
retry = retryHandler.retryRequest(cause, ++executionCount, context);
191-
} catch (IOException e) {
192-
if (isCancelled()) {
193-
// Eating exception, as the request was cancelled
194-
return;
195-
}
196-
cause = e;
197-
retry = retryHandler.retryRequest(cause, ++executionCount, context);
198-
}
199-
if (retry) {
200-
responseHandler.sendRetryMessage(executionCount);
201-
}
202-
}
203-
} catch (Exception e) {
204-
// catch anything else to ensure failure message is propagated
205-
AsyncHttpClient.log.e("AsyncHttpRequest", "Unhandled exception origin cause", e);
206-
cause = new IOException("Unhandled exception: " + e.getMessage());
207-
}
208-
209-
// cleaned up to throw IOException
210-
throw (cause);
211-
}
212-
213169
public boolean isCancelled() {
214170
boolean cancelled = isCancelled.get();
215171
if (cancelled) {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Copyright (c) 2015 Marek Sebera <[email protected]>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package com.loopj.android.http;
17+
18+
import com.loopj.android.http.interfaces.HttpClientProviderInterface;
19+
20+
import java.util.ArrayList;
21+
import java.util.Collection;
22+
23+
import cz.msebera.android.httpclient.Header;
24+
import cz.msebera.android.httpclient.HttpHost;
25+
import cz.msebera.android.httpclient.client.CookieStore;
26+
import cz.msebera.android.httpclient.client.HttpRequestRetryHandler;
27+
import cz.msebera.android.httpclient.conn.HttpClientConnectionManager;
28+
import cz.msebera.android.httpclient.impl.client.CloseableHttpClient;
29+
import cz.msebera.android.httpclient.impl.client.HttpClients;
30+
import cz.msebera.android.httpclient.impl.conn.PoolingHttpClientConnectionManager;
31+
32+
public class DefaultHttpClientProvider implements HttpClientProviderInterface {
33+
34+
protected HttpHost proxy;
35+
protected CookieStore cookieStore;
36+
protected final Collection<? extends Header> commonHeaders = new ArrayList<Header>();
37+
protected HttpRequestRetryHandler retryHandler;
38+
39+
@Override
40+
public final CloseableHttpClient provide() {
41+
return HttpClients.custom()
42+
.setConnectionManager(getConnectionManager())
43+
.setProxy(getProxy())
44+
.setDefaultCookieStore(getCookieStore())
45+
.setUserAgent(getUserAgent())
46+
.setDefaultHeaders(getHeaders())
47+
.setRetryHandler(getRetryHandler())
48+
.build();
49+
}
50+
51+
private HttpRequestRetryHandler getRetryHandler() {
52+
return retryHandler;
53+
}
54+
55+
public String getUserAgent() {
56+
return "AsyncHttpClient ".concat(BuildConfig.VERSION_NAME);
57+
}
58+
59+
public HttpClientConnectionManager getConnectionManager() {
60+
return new PoolingHttpClientConnectionManager();
61+
}
62+
63+
public HttpHost getProxy() {
64+
return proxy;
65+
}
66+
67+
public CookieStore getCookieStore() {
68+
return cookieStore;
69+
}
70+
71+
public Collection<? extends Header> getHeaders() {
72+
return commonHeaders;
73+
}
74+
75+
}

library/src/main/java/com/loopj/android/http/HttpDelete.java

Lines changed: 0 additions & 59 deletions
This file was deleted.

library/src/main/java/com/loopj/android/http/HttpGet.java

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)