Skip to content

Commit a5e55eb

Browse files
committed
Merge branch 'feature/response-headers' of https://github.com/krschultz/android-async-http into response-headers
Conflicts: src/com/loopj/android/http/AsyncHttpResponseHandler.java src/com/loopj/android/http/JsonHttpResponseHandler.java
2 parents 3e12dea + ded7e80 commit a5e55eb

16 files changed

+77
-20
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
_site
22
MANIFEST.MF
3-
*.jar
3+
./*.jar
44
build.num
55
build
66
local.properties

build.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666

6767
<javac
6868
includeantruntime="false"
69-
srcdir="."
69+
srcdir="src"
7070
destdir="${classes.dir}"
7171
classpathref="classpath"
7272
debug="true"
@@ -75,14 +75,12 @@
7575

7676
<!-- Package a jar from compiled class files -->
7777
<target name="jar" depends="git-details,compile">
78-
<delete dir="." includes="**/*.jar" />
79-
<delete file="MANIFEST.MF" />
8078
<manifest file="MANIFEST.MF">
8179
<attribute name="Built-By" value="${user.name}" />
8280
<attribute name="Implementation-Version" value="${package.versionname}"/>
8381
</manifest>
8482

85-
<jar destfile="${package.versionname}.jar" basedir="build/classes" includes="com/loopj/android/http/**/*.class" manifest="MANIFEST.MF" />
83+
<jar destfile="${package.name}-${package.versionname}.jar" basedir="build/classes" includes="com/loopj/android/http/**/*.class" manifest="MANIFEST.MF" />
8684
</target>
8785

8886
<!-- Clean out the build files -->

local.properties.dist

Lines changed: 0 additions & 1 deletion
This file was deleted.

releases/android-async-http-1.2.0.jar

18 KB
Binary file not shown.

releases/android-async-http-1.2.1.jar

18.1 KB
Binary file not shown.

releases/android-async-http-1.3.0.jar

21.8 KB
Binary file not shown.

releases/android-async-http-1.3.1.jar

19.5 KB
Binary file not shown.

releases/android-async-http-1.3.2.jar

22.6 KB
Binary file not shown.

releases/android-async-http-1.4.0.jar

25 KB
Binary file not shown.

releases/android-async-http-1.4.1.jar

25.3 KB
Binary file not shown.

releases/android-async-http-1.4.2.jar

25.5 KB
Binary file not shown.

releases/android-async-http-1.4.3.jar

28.5 KB
Binary file not shown.

src/com/loopj/android/http/AsyncHttpResponseHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ protected Message obtainMessage(int responseMessage, Object response) {
221221
if(handler != null){
222222
msg = this.handler.obtainMessage(responseMessage, response);
223223
}else{
224-
msg = new Message();
224+
msg = Message.obtain();
225225
msg.what = responseMessage;
226226
msg.obj = response;
227227
}
@@ -246,7 +246,7 @@ void sendResponseMessage(HttpResponse response) {
246246
if(status.getStatusCode() >= 300) {
247247
sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), responseBody);
248248
} else {
249-
sendSuccessMessage(status.getStatusCode(),response.getAllHeaders(), responseBody);
249+
sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), responseBody);
250250
}
251251
}
252-
}
252+
}

src/com/loopj/android/http/JsonHttpResponseHandler.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package com.loopj.android.http;
2020

21+
import org.apache.http.HttpStatus;
2122
import org.json.JSONArray;
2223
import org.json.JSONException;
2324
import org.json.JSONObject;
@@ -117,12 +118,16 @@ public void onFailure(Throwable e, JSONArray errorResponse) {}
117118

118119
@Override
119120
protected void sendSuccessMessage(int statusCode, Header[] headers, String responseBody) {
120-
try {
121-
Object jsonResponse = parseResponse(responseBody);
122-
sendMessage(obtainMessage(SUCCESS_JSON_MESSAGE, new Object[]{statusCode, headers, jsonResponse}));
123-
} catch(JSONException e) {
124-
sendFailureMessage(e, responseBody);
125-
}
121+
if (statusCode != HttpStatus.SC_NO_CONTENT){
122+
try {
123+
Object jsonResponse = parseResponse(responseBody);
124+
sendMessage(obtainMessage(SUCCESS_JSON_MESSAGE, new Object[]{statusCode, headers, jsonResponse}));
125+
} catch(JSONException e) {
126+
sendFailureMessage(e, responseBody);
127+
}
128+
} else {
129+
sendMessage(obtainMessage(SUCCESS_JSON_MESSAGE, new Object[]{statusCode, new JSONObject()}));
130+
}
126131
}
127132

128133

src/com/loopj/android/http/RequestParams.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.FileInputStream;
2424
import java.io.FileNotFoundException;
2525
import java.io.UnsupportedEncodingException;
26+
import java.util.ArrayList;
2627
import java.util.LinkedList;
2728
import java.util.List;
2829
import java.util.Map;
@@ -57,6 +58,7 @@ public class RequestParams {
5758

5859
protected ConcurrentHashMap<String, String> urlParams;
5960
protected ConcurrentHashMap<String, FileWrapper> fileParams;
61+
protected ConcurrentHashMap<String, ArrayList<String>> urlParamsWithArray;
6062

6163
/**
6264
* Constructs a new empty <code>RequestParams</code> instance.
@@ -129,6 +131,17 @@ public void put(String key, File file) throws FileNotFoundException {
129131
put(key, new FileInputStream(file), file.getName());
130132
}
131133

134+
/**
135+
* Adds param with more than one value.
136+
* @param key the key name for the new param.
137+
* @param values is the ArrayList with values for the param.
138+
*/
139+
public void put(String key, ArrayList<String> values) {
140+
if(key != null && values != null) {
141+
urlParamsWithArray.put(key, values);
142+
}
143+
}
144+
132145
/**
133146
* Adds an input stream to the request.
134147
* @param key the key name for the new param.
@@ -168,6 +181,7 @@ public void put(String key, InputStream stream, String fileName, String contentT
168181
public void remove(String key){
169182
urlParams.remove(key);
170183
fileParams.remove(key);
184+
urlParamsWithArray.remove(key);
171185
}
172186

173187
@Override
@@ -191,6 +205,20 @@ public String toString() {
191205
result.append("FILE");
192206
}
193207

208+
for(ConcurrentHashMap.Entry<String, ArrayList<String>> entry : urlParamsWithArray.entrySet()) {
209+
if(result.length() > 0)
210+
result.append("&");
211+
212+
ArrayList<String> values = entry.getValue();
213+
for (String value : values) {
214+
if (values.indexOf(value) != 0)
215+
result.append("&");
216+
result.append(entry.getKey());
217+
result.append("=");
218+
result.append(value);
219+
}
220+
}
221+
194222
return result.toString();
195223
}
196224

@@ -224,6 +252,14 @@ public HttpEntity getEntity() {
224252
currentIndex++;
225253
}
226254

255+
// Add dupe params
256+
for(ConcurrentHashMap.Entry<String, ArrayList<String>> entry : urlParamsWithArray.entrySet()) {
257+
ArrayList<String> values = entry.getValue();
258+
for (String value : values) {
259+
multipartEntity.addPart(entry.getKey(), value);
260+
}
261+
}
262+
227263
entity = multipartEntity;
228264
} else {
229265
try {
@@ -239,6 +275,7 @@ public HttpEntity getEntity() {
239275
private void init(){
240276
urlParams = new ConcurrentHashMap<String, String>();
241277
fileParams = new ConcurrentHashMap<String, FileWrapper>();
278+
urlParamsWithArray = new ConcurrentHashMap<String, ArrayList<String>>();
242279
}
243280

244281
protected List<BasicNameValuePair> getParamsList() {
@@ -248,6 +285,13 @@ protected List<BasicNameValuePair> getParamsList() {
248285
lparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
249286
}
250287

288+
for(ConcurrentHashMap.Entry<String, ArrayList<String>> entry : urlParamsWithArray.entrySet()) {
289+
ArrayList<String> values = entry.getValue();
290+
for (String value : values) {
291+
lparams.add(new BasicNameValuePair(entry.getKey(), value));
292+
}
293+
}
294+
251295
return lparams;
252296
}
253297

src/com/loopj/android/http/RetryHandler.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@
2828
import java.net.SocketException;
2929
import java.net.UnknownHostException;
3030
import java.util.HashSet;
31+
import java.util.Iterator;
3132

32-
import javax.net.ssl.SSLHandshakeException;
33+
import javax.net.ssl.SSLException;
3334

3435
import org.apache.http.NoHttpResponseException;
35-
import org.apache.http.client.methods.HttpUriRequest;
3636
import org.apache.http.client.HttpRequestRetryHandler;
37+
import org.apache.http.client.methods.HttpUriRequest;
3738
import org.apache.http.protocol.ExecutionContext;
3839
import org.apache.http.protocol.HttpContext;
3940

@@ -55,7 +56,7 @@ class RetryHandler implements HttpRequestRetryHandler {
5556
// never retry timeouts
5657
exceptionBlacklist.add(InterruptedIOException.class);
5758
// never retry SSL handshake failures
58-
exceptionBlacklist.add(SSLHandshakeException.class);
59+
exceptionBlacklist.add(SSLException.class);
5960
}
6061

6162
private final int maxRetries;
@@ -73,10 +74,10 @@ public boolean retryRequest(IOException exception, int executionCount, HttpConte
7374
if(executionCount > maxRetries) {
7475
// Do not retry if over max retry count
7576
retry = false;
76-
} else if (exceptionBlacklist.contains(exception.getClass())) {
77+
} else if (isInList(exceptionBlacklist, exception)) {
7778
// immediately cancel retry if the error is blacklisted
7879
retry = false;
79-
} else if (exceptionWhitelist.contains(exception.getClass())) {
80+
} else if (isInList(exceptionWhitelist, exception)) {
8081
// immediately retry if error is whitelisted
8182
retry = true;
8283
} else if (!sent) {
@@ -99,4 +100,14 @@ public boolean retryRequest(IOException exception, int executionCount, HttpConte
99100

100101
return retry;
101102
}
103+
104+
protected boolean isInList(HashSet<Class<?>> list, Throwable error) {
105+
Iterator<Class<?>> itr = list.iterator();
106+
while (itr.hasNext()) {
107+
if (itr.next().isInstance(error)) {
108+
return true;
109+
}
110+
}
111+
return false;
112+
}
102113
}

0 commit comments

Comments
 (0)