Skip to content

Commit c2cbe6b

Browse files
committed
Added FileAsyncHttpResponseHandler based on:
android-async-http#134 Modified Request params to allow multiple params with the same name based on android-async-http#136 Fixed unhandled exception AsyncHttpRequest
1 parent 21e182a commit c2cbe6b

File tree

5 files changed

+369
-306
lines changed

5 files changed

+369
-306
lines changed

.classpath

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
44
<classpathentry kind="src" path="src"/>
55
<classpathentry kind="src" path="gen"/>
6-
<classpathentry kind="src" path="examples"/>
76
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
87
<classpathentry kind="output" path="bin/classes"/>
98
</classpath>

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

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -75,33 +75,35 @@ private void makeRequestWithRetries() throws IOException {
7575
boolean retry = true;
7676
IOException cause = null;
7777
HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
78-
while (retry) {
79-
try {
80-
makeRequest();
81-
return;
82-
} catch (UnknownHostException e) {
83-
// switching between WI-FI and mobile data networks can cause a retry which then results in an UnknownHostException
84-
// while the WI-FI is initialising. The retry logic will be invoked here, if this is NOT the first retry
85-
// (to assist in genuine cases of unknown host) which seems better than outright failure
86-
cause = new IOException("UnknownHostException exception: " + e.getMessage());
87-
retry = (executionCount > 0) && retryHandler.retryRequest(cause, ++executionCount, context);
88-
} catch (IOException e) {
89-
cause = e;
90-
retry = retryHandler.retryRequest(cause, ++executionCount, context);
91-
} catch (NullPointerException e) {
92-
// there's a bug in HttpClient 4.0.x that on some occasions causes
93-
// DefaultRequestExecutor to throw an NPE, see
94-
// http://code.google.com/p/android/issues/detail?id=5255
95-
cause = new IOException("NPE in HttpClient: " + e.getMessage());
96-
retry = retryHandler.retryRequest(cause, ++executionCount, context);
97-
} catch (Exception e) {
98-
// catch anything else to ensure failure message is propagated
99-
cause = new IOException("Unhandled exception: " + e.getMessage());
100-
retry = false;
101-
}
102-
if(retry && (responseHandler != null)) {
103-
responseHandler.sendRetryMessage();
78+
try
79+
{
80+
while (retry) {
81+
try {
82+
makeRequest();
83+
return;
84+
} catch (UnknownHostException e) {
85+
// switching between WI-FI and mobile data networks can cause a retry which then results in an UnknownHostException
86+
// while the WI-FI is initialising. The retry logic will be invoked here, if this is NOT the first retry
87+
// (to assist in genuine cases of unknown host) which seems better than outright failure
88+
cause = new IOException("UnknownHostException exception: " + e.getMessage());
89+
retry = (executionCount > 0) && retryHandler.retryRequest(cause, ++executionCount, context);
90+
} catch (IOException e) {
91+
cause = e;
92+
retry = retryHandler.retryRequest(cause, ++executionCount, context);
93+
} catch (NullPointerException e) {
94+
// there's a bug in HttpClient 4.0.x that on some occasions causes
95+
// DefaultRequestExecutor to throw an NPE, see
96+
// http://code.google.com/p/android/issues/detail?id=5255
97+
cause = new IOException("NPE in HttpClient: " + e.getMessage());
98+
retry = retryHandler.retryRequest(cause, ++executionCount, context);
99+
}
100+
if(retry && (responseHandler != null)) {
101+
responseHandler.sendRetryMessage();
102+
}
104103
}
104+
} catch (Exception e) {
105+
// catch anything else to ensure failure message is propagated
106+
cause = new IOException("Unhandled exception: " + e.getMessage());
105107
}
106108

107109
// cleaned up to throw IOException

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void handleMessage(Message msg)
9494
service.handleMessage(msg);
9595
}
9696
}
97-
}
97+
}
9898

9999
/**
100100
* Creates a new AsyncHttpResponseHandler
@@ -279,11 +279,12 @@ byte[] getResponseData(HttpEntity entity) throws IOException {
279279
ByteArrayBuffer buffer = new ByteArrayBuffer((int) contentLength);
280280
try {
281281
byte[] tmp = new byte[BUFFER_SIZE];
282-
int l;
282+
int l, count = 0;
283283
// do not send messages if request has been cancelled
284284
while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) {
285+
count += l;
285286
buffer.append(tmp, 0, l);
286-
sendProgressMessage(buffer.length(), (int) contentLength);
287+
sendProgressMessage(count, (int) contentLength);
287288
}
288289
} finally {
289290
instream.close();
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.loopj.android.http;
2+
3+
import java.io.File;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
8+
import org.apache.http.HttpEntity;
9+
10+
/**
11+
*
12+
* @author sweetlilmre
13+
*
14+
* Implements a response handler that will store the response in the provided
15+
* File object.
16+
*
17+
* Events will be sent as per the AsyncHttpResponseHandler base class, however
18+
* all byte[] values returned will be null.
19+
*/
20+
public class FileAsyncHttpResponseHandler extends AsyncHttpResponseHandler {
21+
22+
private File mFile;
23+
24+
public File getFile() {
25+
return (mFile);
26+
}
27+
28+
public FileAsyncHttpResponseHandler(File file) {
29+
super();
30+
this.mFile = file;
31+
}
32+
33+
@Override
34+
byte[] getResponseData(HttpEntity entity) throws IOException {
35+
if (entity != null) {
36+
InputStream instream = entity.getContent();
37+
long contentLength = entity.getContentLength();
38+
FileOutputStream buffer = new FileOutputStream(this.mFile);
39+
if (instream != null) {
40+
try {
41+
byte[] tmp = new byte[BUFFER_SIZE];
42+
int l, count = 0;;
43+
// do not send messages if request has been cancelled
44+
while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) {
45+
count += l;
46+
buffer.write(tmp, 0, l);
47+
sendProgressMessage(count, (int) contentLength);
48+
}
49+
} finally {
50+
instream.close();
51+
buffer.flush();
52+
buffer.close();
53+
}
54+
}
55+
}
56+
return (null);
57+
}
58+
59+
}

0 commit comments

Comments
 (0)