Skip to content

Commit 9b79fb6

Browse files
committed
Added FileAsyncHttpResponseHandler, Closes android-async-http#134
1 parent 55318eb commit 9b79fb6

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.loopj.android.http;
2+
3+
import android.os.Message;
4+
5+
import org.apache.http.HttpResponse;
6+
import org.apache.http.StatusLine;
7+
import org.apache.http.client.HttpResponseException;
8+
9+
import java.io.File;
10+
import java.io.FileOutputStream;
11+
import java.io.IOException;
12+
import java.io.InputStream;
13+
14+
15+
public class FileAsyncHttpResponseHandler extends AsyncHttpResponseHandler {
16+
17+
private File mFile;
18+
19+
public FileAsyncHttpResponseHandler(File file) {
20+
super();
21+
this.mFile = file;
22+
}
23+
24+
public void onSuccess(File file) {
25+
}
26+
27+
public void onSuccess(int statusCode, File file) {
28+
onSuccess(file);
29+
}
30+
31+
public void onFailure(Throwable e, File response) {
32+
}
33+
34+
35+
protected void sendSuccessMessage(int statusCode, File file) {
36+
sendMessage(obtainMessage(SUCCESS_MESSAGE, new Object[]{statusCode, file}));
37+
}
38+
39+
protected void sendFailureMessage(Throwable e, File file) {
40+
sendMessage(obtainMessage(FAILURE_MESSAGE, new Object[]{e, file}));
41+
}
42+
43+
protected void handleSuccessMessage(int statusCode, File responseBody) {
44+
onSuccess(statusCode, responseBody);
45+
}
46+
47+
protected void handleFailureMessage(Throwable e, File responseBody) {
48+
onFailure(e, responseBody);
49+
}
50+
51+
// Methods which emulate android's Handler and Message methods
52+
protected void handleMessage(Message msg) {
53+
Object[] response;
54+
switch (msg.what) {
55+
case SUCCESS_MESSAGE:
56+
response = (Object[]) msg.obj;
57+
handleSuccessMessage(((Integer) response[0]).intValue(), (File) response[1]);
58+
break;
59+
case FAILURE_MESSAGE:
60+
response = (Object[]) msg.obj;
61+
handleFailureMessage((Throwable) response[0], (File) response[1]);
62+
break;
63+
default:
64+
super.handleMessage(msg);
65+
break;
66+
}
67+
}
68+
69+
@Override
70+
protected void sendResponseMessage(HttpResponse response) {
71+
StatusLine status = response.getStatusLine();
72+
73+
try {
74+
FileOutputStream buffer = new FileOutputStream(this.mFile);
75+
InputStream is = response.getEntity().getContent();
76+
77+
int nRead;
78+
byte[] data = new byte[16384];
79+
80+
while ((nRead = is.read(data, 0, data.length)) != -1)
81+
buffer.write(data, 0, nRead);
82+
83+
buffer.flush();
84+
buffer.close();
85+
86+
} catch (IOException e) {
87+
sendFailureMessage(e, this.mFile);
88+
}
89+
90+
if (status.getStatusCode() >= 300) {
91+
sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), this.mFile);
92+
} else {
93+
sendSuccessMessage(status.getStatusCode(), this.mFile);
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)