Skip to content

Commit

Permalink
Merge branch 'feature/response-headers' of https://github.com/krschul…
Browse files Browse the repository at this point in the history
…tz/android-async-http into response-headers

Conflicts:
	src/com/loopj/android/http/AsyncHttpResponseHandler.java
	src/com/loopj/android/http/JsonHttpResponseHandler.java
  • Loading branch information
p-m-j committed Mar 28, 2013
2 parents 3e12dea + ded7e80 commit a5e55eb
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
_site
MANIFEST.MF
*.jar
./*.jar
build.num
build
local.properties
Expand Down
6 changes: 2 additions & 4 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

<javac
includeantruntime="false"
srcdir="."
srcdir="src"
destdir="${classes.dir}"
classpathref="classpath"
debug="true"
Expand All @@ -75,14 +75,12 @@

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

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

<!-- Clean out the build files -->
Expand Down
1 change: 0 additions & 1 deletion local.properties.dist

This file was deleted.

Binary file added releases/android-async-http-1.2.0.jar
Binary file not shown.
Binary file added releases/android-async-http-1.2.1.jar
Binary file not shown.
Binary file added releases/android-async-http-1.3.0.jar
Binary file not shown.
Binary file added releases/android-async-http-1.3.1.jar
Binary file not shown.
Binary file added releases/android-async-http-1.3.2.jar
Binary file not shown.
Binary file added releases/android-async-http-1.4.0.jar
Binary file not shown.
Binary file added releases/android-async-http-1.4.1.jar
Binary file not shown.
Binary file added releases/android-async-http-1.4.2.jar
Binary file not shown.
Binary file added releases/android-async-http-1.4.3.jar
Binary file not shown.
6 changes: 3 additions & 3 deletions src/com/loopj/android/http/AsyncHttpResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ protected Message obtainMessage(int responseMessage, Object response) {
if(handler != null){
msg = this.handler.obtainMessage(responseMessage, response);
}else{
msg = new Message();
msg = Message.obtain();
msg.what = responseMessage;
msg.obj = response;
}
Expand All @@ -246,7 +246,7 @@ void sendResponseMessage(HttpResponse response) {
if(status.getStatusCode() >= 300) {
sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), responseBody);
} else {
sendSuccessMessage(status.getStatusCode(),response.getAllHeaders(), responseBody);
sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), responseBody);
}
}
}
}
17 changes: 11 additions & 6 deletions src/com/loopj/android/http/JsonHttpResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.loopj.android.http;

import org.apache.http.HttpStatus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -117,12 +118,16 @@ public void onFailure(Throwable e, JSONArray errorResponse) {}

@Override
protected void sendSuccessMessage(int statusCode, Header[] headers, String responseBody) {
try {
Object jsonResponse = parseResponse(responseBody);
sendMessage(obtainMessage(SUCCESS_JSON_MESSAGE, new Object[]{statusCode, headers, jsonResponse}));
} catch(JSONException e) {
sendFailureMessage(e, responseBody);
}
if (statusCode != HttpStatus.SC_NO_CONTENT){
try {
Object jsonResponse = parseResponse(responseBody);
sendMessage(obtainMessage(SUCCESS_JSON_MESSAGE, new Object[]{statusCode, headers, jsonResponse}));
} catch(JSONException e) {
sendFailureMessage(e, responseBody);
}
} else {
sendMessage(obtainMessage(SUCCESS_JSON_MESSAGE, new Object[]{statusCode, new JSONObject()}));
}
}


Expand Down
44 changes: 44 additions & 0 deletions src/com/loopj/android/http/RequestParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -57,6 +58,7 @@ public class RequestParams {

protected ConcurrentHashMap<String, String> urlParams;
protected ConcurrentHashMap<String, FileWrapper> fileParams;
protected ConcurrentHashMap<String, ArrayList<String>> urlParamsWithArray;

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

/**
* Adds param with more than one value.
* @param key the key name for the new param.
* @param values is the ArrayList with values for the param.
*/
public void put(String key, ArrayList<String> values) {
if(key != null && values != null) {
urlParamsWithArray.put(key, values);
}
}

/**
* Adds an input stream to the request.
* @param key the key name for the new param.
Expand Down Expand Up @@ -168,6 +181,7 @@ public void put(String key, InputStream stream, String fileName, String contentT
public void remove(String key){
urlParams.remove(key);
fileParams.remove(key);
urlParamsWithArray.remove(key);
}

@Override
Expand All @@ -191,6 +205,20 @@ public String toString() {
result.append("FILE");
}

for(ConcurrentHashMap.Entry<String, ArrayList<String>> entry : urlParamsWithArray.entrySet()) {
if(result.length() > 0)
result.append("&");

ArrayList<String> values = entry.getValue();
for (String value : values) {
if (values.indexOf(value) != 0)
result.append("&");
result.append(entry.getKey());
result.append("=");
result.append(value);
}
}

return result.toString();
}

Expand Down Expand Up @@ -224,6 +252,14 @@ public HttpEntity getEntity() {
currentIndex++;
}

// Add dupe params
for(ConcurrentHashMap.Entry<String, ArrayList<String>> entry : urlParamsWithArray.entrySet()) {
ArrayList<String> values = entry.getValue();
for (String value : values) {
multipartEntity.addPart(entry.getKey(), value);
}
}

entity = multipartEntity;
} else {
try {
Expand All @@ -239,6 +275,7 @@ public HttpEntity getEntity() {
private void init(){
urlParams = new ConcurrentHashMap<String, String>();
fileParams = new ConcurrentHashMap<String, FileWrapper>();
urlParamsWithArray = new ConcurrentHashMap<String, ArrayList<String>>();
}

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

for(ConcurrentHashMap.Entry<String, ArrayList<String>> entry : urlParamsWithArray.entrySet()) {
ArrayList<String> values = entry.getValue();
for (String value : values) {
lparams.add(new BasicNameValuePair(entry.getKey(), value));
}
}

return lparams;
}

Expand Down
21 changes: 16 additions & 5 deletions src/com/loopj/android/http/RetryHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.HashSet;
import java.util.Iterator;

import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLException;

import org.apache.http.NoHttpResponseException;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;

Expand All @@ -55,7 +56,7 @@ class RetryHandler implements HttpRequestRetryHandler {
// never retry timeouts
exceptionBlacklist.add(InterruptedIOException.class);
// never retry SSL handshake failures
exceptionBlacklist.add(SSLHandshakeException.class);
exceptionBlacklist.add(SSLException.class);
}

private final int maxRetries;
Expand All @@ -73,10 +74,10 @@ public boolean retryRequest(IOException exception, int executionCount, HttpConte
if(executionCount > maxRetries) {
// Do not retry if over max retry count
retry = false;
} else if (exceptionBlacklist.contains(exception.getClass())) {
} else if (isInList(exceptionBlacklist, exception)) {
// immediately cancel retry if the error is blacklisted
retry = false;
} else if (exceptionWhitelist.contains(exception.getClass())) {
} else if (isInList(exceptionWhitelist, exception)) {
// immediately retry if error is whitelisted
retry = true;
} else if (!sent) {
Expand All @@ -99,4 +100,14 @@ public boolean retryRequest(IOException exception, int executionCount, HttpConte

return retry;
}

protected boolean isInList(HashSet<Class<?>> list, Throwable error) {
Iterator<Class<?>> itr = list.iterator();
while (itr.hasNext()) {
if (itr.next().isInstance(error)) {
return true;
}
}
return false;
}
}

0 comments on commit a5e55eb

Please sign in to comment.