Skip to content

Commit 9e1d6d0

Browse files
committed
Refactor response handler to work better with requests that didnt originate in the ui thread
1 parent 9349b47 commit 9e1d6d0

File tree

4 files changed

+75
-34
lines changed

4 files changed

+75
-34
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void run() {
5656
} catch (IOException e) {
5757
if(responseHandler != null) {
5858
responseHandler.sendFinishMessage();
59-
responseHandler.sendErrorMessage(e);
59+
responseHandler.sendFailureMessage(e);
6060
}
6161
}
6262
}

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

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,49 +31,75 @@
3131
import android.os.Message;
3232
import android.os.Looper;
3333

34-
public class AsyncHttpResponseHandler extends Handler {
35-
private static final int RESPONSE_MESSAGE = 0;
36-
private static final int ERROR_MESSAGE = 1;
34+
public class AsyncHttpResponseHandler {
35+
private static final int SUCCESS_MESSAGE = 0;
36+
private static final int FAILURE_MESSAGE = 1;
3737
private static final int START_MESSAGE = 2;
3838
private static final int FINISH_MESSAGE = 3;
3939

40+
private Handler handler;
41+
4042
public AsyncHttpResponseHandler() {
41-
super(Looper.getMainLooper());
43+
// Set up a handler to post events back to the correct thread if possible
44+
if(Looper.myLooper() != null) {
45+
handler = new Handler(){
46+
public void handleMessage(Message msg){
47+
AsyncHttpResponseHandler.this.handleMessage(msg);
48+
}
49+
};
50+
}
4251
}
4352

44-
public void sendStartMessage() {
45-
sendMessage(obtainMessage(START_MESSAGE));
46-
}
47-
48-
public void sendFinishMessage() {
49-
sendMessage(obtainMessage(FINISH_MESSAGE));
50-
}
5153

54+
// Pre-processing of messages (in background thread)
5255
public void sendResponseMessage(HttpResponse response) {
5356
StatusLine status = response.getStatusLine();
5457
if(status.getStatusCode() >= 300) {
55-
sendErrorMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()));
58+
sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()));
5659
} else {
5760
try {
58-
sendMessage(obtainMessage(RESPONSE_MESSAGE, getResponseBody(response)));
61+
sendSuccessMessage(getResponseBody(response));
5962
} catch(IOException e) {
60-
sendErrorMessage(e);
63+
sendFailureMessage(e);
6164
}
6265
}
6366
}
6467

65-
public void sendErrorMessage(Throwable e) {
66-
sendMessage(obtainMessage(ERROR_MESSAGE, e));
68+
public void sendSuccessMessage(String responseBody) {
69+
sendMessage(obtainMessage(SUCCESS_MESSAGE, responseBody));
70+
}
71+
72+
public void sendFailureMessage(Throwable e) {
73+
sendMessage(obtainMessage(FAILURE_MESSAGE, e));
74+
}
75+
76+
public void sendStartMessage() {
77+
sendMessage(obtainMessage(START_MESSAGE, null));
78+
}
79+
80+
public void sendFinishMessage() {
81+
sendMessage(obtainMessage(FINISH_MESSAGE, null));
82+
}
83+
84+
85+
// Pre-processing of messages (in original calling thread)
86+
protected void handleSuccessMessage(String responseBody) {
87+
onSuccess(responseBody);
88+
}
89+
90+
protected void handleFailureMessage(Throwable e) {
91+
onFailure(e);
6792
}
6893

69-
@Override
70-
public void handleMessage(Message msg) {
94+
95+
// Utility functions
96+
protected void handleMessage(Message msg) {
7197
switch(msg.what) {
72-
case RESPONSE_MESSAGE:
73-
handleResponseMessage((String)msg.obj);
98+
case SUCCESS_MESSAGE:
99+
handleSuccessMessage((String)msg.obj);
74100
break;
75-
case ERROR_MESSAGE:
76-
handleErrorMessage((Throwable)msg.obj);
101+
case FAILURE_MESSAGE:
102+
handleFailureMessage((Throwable)msg.obj);
77103
break;
78104
case START_MESSAGE:
79105
onStart();
@@ -84,14 +110,6 @@ public void handleMessage(Message msg) {
84110
}
85111
}
86112

87-
protected void handleResponseMessage(String responseBody) {
88-
onSuccess(responseBody);
89-
}
90-
91-
protected void handleErrorMessage(Throwable e) {
92-
onFailure(e);
93-
}
94-
95113
protected String getResponseBody(HttpResponse response) throws IOException {
96114
HttpEntity entity = null;
97115
HttpEntity temp = response.getEntity();
@@ -102,6 +120,26 @@ protected String getResponseBody(HttpResponse response) throws IOException {
102120
return EntityUtils.toString(entity);
103121
}
104122

123+
protected void sendMessage(Message msg) {
124+
if(handler != null){
125+
handler.sendMessage(msg);
126+
} else {
127+
handleMessage(msg);
128+
}
129+
}
130+
131+
protected Message obtainMessage(int responseMessage, Object response) {
132+
Message msg = null;
133+
if(handler != null){
134+
msg = this.handler.obtainMessage(responseMessage, response);
135+
}else{
136+
msg = new Message();
137+
msg.what = responseMessage;
138+
msg.obj = response;
139+
}
140+
return msg;
141+
}
142+
105143
// Public callbacks
106144
public void onStart() {}
107145
public void onFinish() {}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727

2828
public class JsonHttpResponseHandler extends AsyncHttpResponseHandler {
2929
@Override
30-
protected void handleResponseMessage(String responseBody) {
31-
super.handleResponseMessage(responseBody);
30+
public void handleSuccessMessage(String responseBody) {
31+
super.handleSuccessMessage(responseBody);
3232

3333
try {
3434
Object jsonResponse = new JSONTokener(responseBody).nextValue();

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,14 @@ public RequestParams(Map<String, String> source) {
4848

4949
public RequestParams(String key, String value) {
5050
init();
51+
5152
put(key, value);
5253
}
5354

5455
public void put(String key, String value){
55-
urlParams.put(key,value);
56+
if(key != null && value != null) {
57+
urlParams.put(key,value);
58+
}
5659
}
5760

5861
public void remove(String key){

0 commit comments

Comments
 (0)