Skip to content

Commit 534a057

Browse files
committed
Merge pull request android-async-http#671 from zxw1962/master
Fix comments typo, rename param, refine obtainMessage
2 parents 12d75a8 + d0ce403 commit 534a057

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Android Asynchronous Http Client
3+
Copyright (c) 2011 James Smith <[email protected]>
4+
http://loopj.com
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package com.loopj.android.http;
20+
21+
/**
22+
* Internal class, used to make some asserts, throw AssertError if asserts fail.
23+
*/
24+
class AssertUtils {
25+
26+
private AssertUtils() {
27+
}
28+
29+
public static void asserts(final boolean expression, final String failedMessage) {
30+
if (!expression) {
31+
throw new AssertionError(failedMessage);
32+
}
33+
}
34+
}

library/src/main/java/com/loopj/android/http/AsyncHttpClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public AsyncHttpClient(int httpPort, int httpsPort) {
169169
/**
170170
* Creates new AsyncHttpClient using given params
171171
*
172-
* @param fixNoHttpResponseException Whether to fix or not issue, by ommiting SSL verification
172+
* @param fixNoHttpResponseException Whether to fix issue or not, by omitting SSL verification
173173
* @param httpPort HTTP port to be used, must be greater than 0
174174
* @param httpsPort HTTPS port to be used, must be greater than 0
175175
*/
@@ -180,7 +180,7 @@ public AsyncHttpClient(boolean fixNoHttpResponseException, int httpPort, int htt
180180
/**
181181
* Returns default instance of SchemeRegistry
182182
*
183-
* @param fixNoHttpResponseException Whether to fix or not issue, by ommiting SSL verification
183+
* @param fixNoHttpResponseException Whether to fix issue or not, by omitting SSL verification
184184
* @param httpPort HTTP port to be used, must be greater than 0
185185
* @param httpsPort HTTPS port to be used, must be greater than 0
186186
*/

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -145,23 +145,23 @@ public boolean getUseSynchronousMode() {
145145
}
146146

147147
@Override
148-
public void setUseSynchronousMode(boolean value) {
148+
public void setUseSynchronousMode(boolean sync) {
149149
// A looper must be prepared before setting asynchronous mode.
150-
if (!value && this.looper == null) {
151-
value = true;
150+
if (!sync && looper == null) {
151+
sync = true;
152152
Log.w(LOG_TAG, "Current thread has not called Looper.prepare(). Forcing synchronous mode.");
153153
}
154154

155155
// If using asynchronous mode.
156-
if (!value && handler == null) {
156+
if (!sync && handler == null) {
157157
// Create a handler on current thread to submit tasks
158-
handler = new ResponderHandler(this, this.looper);
159-
} else if (value && handler != null) {
158+
handler = new ResponderHandler(this, looper);
159+
} else if (sync && handler != null) {
160160
// TODO: Consider adding a flag to remove all queued messages.
161161
handler = null;
162162
}
163163

164-
useSynchronousMode = value;
164+
useSynchronousMode = sync;
165165
}
166166

167167
/**
@@ -357,6 +357,7 @@ protected void sendMessage(Message msg) {
357357
if (getUseSynchronousMode() || handler == null) {
358358
handleMessage(msg);
359359
} else if (!Thread.currentThread().isInterrupted()) { // do not send messages if request has been cancelled
360+
AssertUtils.asserts(handler != null, "handler should not be null!");
360361
handler.sendMessage(msg);
361362
}
362363
}
@@ -373,6 +374,7 @@ protected void postRunnable(Runnable runnable) {
373374
runnable.run();
374375
} else {
375376
// Otherwise, run on provided handler
377+
AssertUtils.asserts(handler != null, "handler should not be null!");
376378
handler.post(runnable);
377379
}
378380
}
@@ -386,17 +388,7 @@ protected void postRunnable(Runnable runnable) {
386388
* @return Message instance, should not be null
387389
*/
388390
protected Message obtainMessage(int responseMessageId, Object responseMessageData) {
389-
Message msg;
390-
if (handler == null) {
391-
msg = Message.obtain();
392-
if (msg != null) {
393-
msg.what = responseMessageId;
394-
msg.obj = responseMessageData;
395-
}
396-
} else {
397-
msg = Message.obtain(handler, responseMessageId, responseMessageData);
398-
}
399-
return msg;
391+
return Message.obtain(handler, responseMessageId, responseMessageData);
400392
}
401393

402394
@Override

0 commit comments

Comments
 (0)