@@ -49,6 +49,9 @@ public class AsyncHttpResponseHandler {
4949
5050 private Handler handler ;
5151
52+ /**
53+ * Creates a new AsyncHttpResponseHandler
54+ */
5255 public AsyncHttpResponseHandler () {
5356 // Set up a handler to post events back to the correct thread if possible
5457 if (Looper .myLooper () != null ) {
@@ -61,44 +64,58 @@ public void handleMessage(Message msg){
6164 }
6265
6366
64- // Pre-processing of messages (in background thread)
65- public void sendResponseMessage (HttpResponse response ) {
66- StatusLine status = response .getStatusLine ();
67- if (status .getStatusCode () >= 300 ) {
68- sendFailureMessage (new HttpResponseException (status .getStatusCode (), status .getReasonPhrase ()));
69- } else {
70- try {
71- HttpEntity entity = null ;
72- HttpEntity temp = response .getEntity ();
73- if (temp != null ) {
74- entity = new BufferedHttpEntity (temp );
75- }
67+ //
68+ // Callbacks to be overridden, typically anonymously
69+ //
70+
71+ /**
72+ * Fired when the request is started, override to handle in your own code
73+ */
74+ public void onStart () {}
75+
76+ /**
77+ * Fired in all cases when the request is finished, after both success and failure, override to handle in your own code
78+ */
79+ public void onFinish () {}
80+
81+ /**
82+ * Fired when a request returns successfully, override to handle in your own code
83+ * @param content the body of the HTTP response from the server
84+ */
85+ public void onSuccess (String content ) {}
86+
87+ /**
88+ * Fired when a request fails to complete, override to handle in your own code
89+ * @param error the underlying cause of the failure
90+ */
91+ public void onFailure (Throwable error ) {}
7692
77- sendSuccessMessage (EntityUtils .toString (entity ));
78- } catch (IOException e ) {
79- sendFailureMessage (e );
80- }
81- }
82- }
8393
84- public void sendSuccessMessage (String responseBody ) {
94+ //
95+ // Pre-processing of messages (executes in background threadpool thread)
96+ //
97+
98+ protected void sendSuccessMessage (String responseBody ) {
8599 sendMessage (obtainMessage (SUCCESS_MESSAGE , responseBody ));
86100 }
87101
88- public void sendFailureMessage (Throwable e ) {
102+ protected void sendFailureMessage (Throwable e ) {
89103 sendMessage (obtainMessage (FAILURE_MESSAGE , e ));
90104 }
91105
92- public void sendStartMessage () {
106+ protected void sendStartMessage () {
93107 sendMessage (obtainMessage (START_MESSAGE , null ));
94108 }
95109
96- public void sendFinishMessage () {
110+ protected void sendFinishMessage () {
97111 sendMessage (obtainMessage (FINISH_MESSAGE , null ));
98112 }
99113
100114
101- // Pre-processing of messages (in original calling thread)
115+ //
116+ // Pre-processing of messages (in original calling thread, typically the UI thread)
117+ //
118+
102119 protected void handleSuccessMessage (String responseBody ) {
103120 onSuccess (responseBody );
104121 }
@@ -108,7 +125,8 @@ protected void handleFailureMessage(Throwable e) {
108125 }
109126
110127
111- // Utility functions
128+
129+ // Methods which emulate android's Handler and Message methods
112130 protected void handleMessage (Message msg ) {
113131 switch (msg .what ) {
114132 case SUCCESS_MESSAGE :
@@ -147,9 +165,23 @@ protected Message obtainMessage(int responseMessage, Object response) {
147165 }
148166
149167
150- // Public callbacks
151- public void onStart () {}
152- public void onFinish () {}
153- public void onSuccess (String content ) {}
154- public void onFailure (Throwable error ) {}
168+ // Interface to AsyncHttpRequest
169+ void sendResponseMessage (HttpResponse response ) {
170+ StatusLine status = response .getStatusLine ();
171+ if (status .getStatusCode () >= 300 ) {
172+ sendFailureMessage (new HttpResponseException (status .getStatusCode (), status .getReasonPhrase ()));
173+ } else {
174+ try {
175+ HttpEntity entity = null ;
176+ HttpEntity temp = response .getEntity ();
177+ if (temp != null ) {
178+ entity = new BufferedHttpEntity (temp );
179+ }
180+
181+ sendSuccessMessage (EntityUtils .toString (entity ));
182+ } catch (IOException e ) {
183+ sendFailureMessage (e );
184+ }
185+ }
186+ }
155187}
0 commit comments