Skip to content

Commit 7b35e5e

Browse files
committed
Code style
1 parent 19e73fa commit 7b35e5e

File tree

6 files changed

+83
-89
lines changed

6 files changed

+83
-89
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void run() {
5555
responseHandler.sendFailureMessage(0, null, null, e);
5656
}
5757
}
58-
58+
5959
if (responseHandler != null) {
6060
responseHandler.sendFinishMessage();
6161
}
@@ -83,8 +83,7 @@ private void makeRequestWithRetries() throws IOException {
8383
boolean retry = true;
8484
IOException cause = null;
8585
HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
86-
try
87-
{
86+
try {
8887
while (retry) {
8988
try {
9089
makeRequest();
@@ -105,16 +104,16 @@ private void makeRequestWithRetries() throws IOException {
105104
cause = e;
106105
retry = retryHandler.retryRequest(cause, ++executionCount, context);
107106
}
108-
if(retry && (responseHandler != null)) {
107+
if (retry && (responseHandler != null)) {
109108
responseHandler.sendRetryMessage();
110109
}
111110
}
112111
} catch (Exception e) {
113112
// catch anything else to ensure failure message is propagated
114113
cause = new IOException("Unhandled exception: " + e.getMessage());
115114
}
116-
115+
117116
// cleaned up to throw IOException
118-
throw(cause);
117+
throw (cause);
119118
}
120119
}

library/src/com/loopj/android/http/RequestHandle.java

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,49 @@
44

55
/**
66
* A Handle to an AsyncRequest which can be used to cancel a running request.
7-
*
87
*/
98
public class RequestHandle {
10-
private final Future<?> request;
11-
12-
public RequestHandle(Future<?> request) {
13-
this.request = request;
14-
}
15-
16-
/**
17-
* Attempts to cancel this request. This attempt will fail if the request has
18-
* already completed, has already been cancelled, or could not be cancelled
19-
* for some other reason. If successful, and this request has not started
20-
* when cancel is called, this request should never run. If the request has
21-
* already started, then the mayInterruptIfRunning parameter determines
22-
* whether the thread executing this request should be interrupted in an
23-
* attempt to stop the request.
24-
*
25-
* After this method returns, subsequent calls to isDone() will always
26-
* return true. Subsequent calls to isCancelled() will always return true
27-
* if this method returned true.
28-
*
29-
* @param mayInterruptIfRunning true if the thread executing this request should be interrupted; otherwise, in-progress requests are allowed to complete
30-
* @return false if the request could not be cancelled, typically because it has already completed normally; true otherwise
31-
*/
32-
public boolean cancel(boolean mayInterruptIfRunning) {
33-
if (this.request == null) {
34-
return false;
35-
}
36-
return request.cancel(mayInterruptIfRunning);
37-
}
38-
39-
/**
40-
* Returns true if this task completed. Completion may be due to normal termination, an exception, or cancellation -- in all of these cases, this method will return true.
41-
* @return true if this task completed
42-
*/
43-
public boolean isFinished() {
44-
if (this.request == null) {
45-
return true;
46-
}
47-
return request.isDone();
48-
}
49-
50-
/**
51-
* Returns true if this task was cancelled before it completed normally.
52-
* @return true if this task was cancelled before it completed
53-
*/
54-
public boolean isCancelled() {
55-
if (this.request == null) {
56-
return false;
57-
}
58-
return request.isCancelled();
59-
}
9+
private final Future<?> request;
10+
11+
public RequestHandle(Future<?> request) {
12+
this.request = request;
13+
}
14+
15+
/**
16+
* Attempts to cancel this request. This attempt will fail if the request has
17+
* already completed, has already been cancelled, or could not be cancelled
18+
* for some other reason. If successful, and this request has not started
19+
* when cancel is called, this request should never run. If the request has
20+
* already started, then the mayInterruptIfRunning parameter determines
21+
* whether the thread executing this request should be interrupted in an
22+
* attempt to stop the request.
23+
* <p/>
24+
* After this method returns, subsequent calls to isDone() will always
25+
* return true. Subsequent calls to isCancelled() will always return true
26+
* if this method returned true.
27+
*
28+
* @param mayInterruptIfRunning true if the thread executing this request should be interrupted; otherwise, in-progress requests are allowed to complete
29+
* @return false if the request could not be cancelled, typically because it has already completed normally; true otherwise
30+
*/
31+
public boolean cancel(boolean mayInterruptIfRunning) {
32+
return this.request != null && request.cancel(mayInterruptIfRunning);
33+
}
34+
35+
/**
36+
* Returns true if this task completed. Completion may be due to normal termination, an exception, or cancellation -- in all of these cases, this method will return true.
37+
*
38+
* @return true if this task completed
39+
*/
40+
public boolean isFinished() {
41+
return this.request == null || request.isDone();
42+
}
43+
44+
/**
45+
* Returns true if this task was cancelled before it completed normally.
46+
*
47+
* @return true if this task was cancelled before it completed
48+
*/
49+
public boolean isCancelled() {
50+
return this.request != null && request.isCancelled();
51+
}
6052
}

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

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.UnsupportedEncodingException;
3232
import java.util.ArrayList;
3333
import java.util.Collections;
34+
import java.util.HashMap;
3435
import java.util.HashSet;
3536
import java.util.LinkedList;
3637
import java.util.List;
@@ -52,7 +53,7 @@
5253
* params.put("profile_picture", new File("pic.jpg")); // Upload a File
5354
* params.put("profile_picture2", someInputStream); // Upload an InputStream
5455
* params.put("profile_picture3", new ByteArrayInputStream(someBytes)); // Upload some bytes
55-
*
56+
*
5657
* Map<String, String> map = new HashMap<String, String>();
5758
* map.put("first_name", "James");
5859
* map.put("last_name", "Smith");
@@ -62,15 +63,15 @@
6263
* set.add("music");
6364
* set.add("art");
6465
* params.put("like", set); // url params: "like=music&like=art"
65-
*
66+
*
6667
* List<String> list = new ArrayList<String>(); // Ordered collection
6768
* list.add("Java");
6869
* list.add("C");
6970
* params.put("languages", list); // url params: "languages[]=Java&languages[]=C"
70-
*
71+
*
7172
* String[] colors = { "blue", "yellow" }; // Ordered collection
7273
* params.put("colors", colors); // url params: "colors[]=blue&colors[]=yellow"
73-
*
74+
*
7475
* List<Map<String, String>> listOfMaps = new ArrayList<Map<String, String>>();
7576
* Map<String, String> user1 = new HashMap<String, String>();
7677
* user1.put("age", "30");
@@ -88,8 +89,6 @@
8889
*/
8990
public class RequestParams {
9091

91-
private static final String LOG_TAG = "RequestParams";
92-
9392
protected boolean isRepeatable = false;
9493
protected ConcurrentHashMap<String, String> urlParams;
9594
protected ConcurrentHashMap<String, StreamWrapper> streamParams;
@@ -100,7 +99,7 @@ public class RequestParams {
10099
* Constructs a new empty <code>RequestParams</code> instance.
101100
*/
102101
public RequestParams() {
103-
init();
102+
this((Map<String, String>) null);
104103
}
105104

106105
/**
@@ -111,9 +110,10 @@ public RequestParams() {
111110
*/
112111
public RequestParams(Map<String, String> source) {
113112
init();
114-
115-
for (Map.Entry<String, String> entry : source.entrySet()) {
116-
put(entry.getKey(), entry.getValue());
113+
if (source != null) {
114+
for (Map.Entry<String, String> entry : source.entrySet()) {
115+
put(entry.getKey(), entry.getValue());
116+
}
117117
}
118118
}
119119

@@ -124,10 +124,10 @@ public RequestParams(Map<String, String> source) {
124124
* @param key the key name for the intial param.
125125
* @param value the value string for the initial param.
126126
*/
127-
public RequestParams(String key, String value) {
128-
init();
129-
130-
put(key, value);
127+
public RequestParams(final String key, final String value) {
128+
this(new HashMap<String, String>() {{
129+
put(key, value);
130+
}});
131131
}
132132

133133
/**
@@ -224,17 +224,19 @@ public void put(String key, InputStream stream, String name, String contentType)
224224

225225
/**
226226
* Adds param with non-string value (e.g. Map, List, Set).
227+
*
227228
* @param key the key name for the new param.
228229
* @param value the non-string value object for the new param.
229230
*/
230-
public void put(String key, Object value) {
231+
public void put(String key, Object value) {
231232
if (key != null && value != null) {
232233
urlParamsWithObjects.put(key, value);
233234
}
234235
}
235-
236+
236237
/**
237238
* Adds string value to param which can have more than one value.
239+
*
238240
* @param key the key name for the param, either existing or new.
239241
* @param value the value string for the new param.
240242
*/
@@ -253,7 +255,7 @@ public void add(String key, String value) {
253255
}
254256
}
255257
}
256-
258+
257259
/**
258260
* Removes a parameter from the request.
259261
*
@@ -300,7 +302,7 @@ public String toString() {
300302
for (BasicNameValuePair kv : params) {
301303
if (result.length() > 0)
302304
result.append("&");
303-
305+
304306
result.append(kv.getName());
305307
result.append("=");
306308
result.append(kv.getValue());
@@ -309,7 +311,7 @@ public String toString() {
309311
return result.toString();
310312
}
311313

312-
public void setHttpEntityIsRepeatable(boolean isRepeatable){
314+
public void setHttpEntityIsRepeatable(boolean isRepeatable) {
313315
this.isRepeatable = isRepeatable;
314316
}
315317

@@ -348,7 +350,7 @@ private HttpEntity createMultipartEntity(AsyncHttpResponseHandler progressHandle
348350
// Add non-string params
349351
List<BasicNameValuePair> params = getParamsList(null, urlParamsWithObjects);
350352
for (BasicNameValuePair kv : params) {
351-
entity.addPart(kv.getName(), kv.getValue());
353+
entity.addPart(kv.getName(), kv.getValue());
352354
}
353355

354356
// Add stream params
@@ -422,7 +424,7 @@ private List<BasicNameValuePair> getParamsList(String key, Object value) {
422424
}
423425
return params;
424426
}
425-
427+
426428
protected String getParamString() {
427429
return URLEncodedUtils.format(getParamsList(), HTTP.UTF_8);
428430
}
@@ -431,7 +433,7 @@ private static class FileWrapper {
431433
public File file;
432434
public String contentType;
433435

434-
public FileWrapper(File file, String contentType) {
436+
public FileWrapper(File file, String contentType) {
435437
this.file = file;
436438
this.contentType = contentType;
437439
}

library/src/com/loopj/android/http/SimpleMultipartEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public boolean isChunked() {
243243
return false;
244244
}
245245

246-
public void setIsRepeatable(boolean isRepeatable){
246+
public void setIsRepeatable(boolean isRepeatable) {
247247
this.isRepeatable = isRepeatable;
248248
}
249249

library/src/com/loopj/android/http/SyncHttpClient.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.loopj.android.http;
22

33
import android.content.Context;
4+
45
import org.apache.http.client.methods.HttpUriRequest;
56
import org.apache.http.conn.scheme.SchemeRegistry;
67
import org.apache.http.impl.client.DefaultHttpClient;
@@ -56,9 +57,9 @@ public SyncHttpClient(SchemeRegistry schemeRegistry) {
5657

5758
@Override
5859
protected RequestHandle sendRequest(DefaultHttpClient client,
59-
HttpContext httpContext, HttpUriRequest uriRequest,
60-
String contentType, AsyncHttpResponseHandler responseHandler,
61-
Context context) {
60+
HttpContext httpContext, HttpUriRequest uriRequest,
61+
String contentType, AsyncHttpResponseHandler responseHandler,
62+
Context context) {
6263
if (contentType != null) {
6364
uriRequest.addHeader("Content-Type", contentType);
6465
}
@@ -69,7 +70,7 @@ protected RequestHandle sendRequest(DefaultHttpClient client,
6970
* will execute the request directly
7071
*/
7172
new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler).run();
72-
73+
7374
// Return a Request Handle that cannot be used to cancel the request
7475
// because it is already complete by the time this returns
7576
return new RequestHandle(null);

library/src/com/loopj/android/http/TextHttpResponseHandler.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,19 @@ public void onFailure(int statusCode, Header[] headers, String responseBody, Thr
8888
* Fired when a request returns successfully, override to handle in your own
8989
* code
9090
*
91-
* @param statusCode the status code of the response
92-
* @param headers HTTP response headers
91+
* @param statusCode the status code of the response
92+
* @param headers HTTP response headers
9393
* @param responseBody the body of the HTTP response from the server
9494
*/
9595
@Override
9696
public void onSuccess(int statusCode, Header[] headers, String responseBody) {
97-
onSuccess( statusCode, responseBody );
97+
onSuccess(statusCode, responseBody);
9898
}
9999

100100
@Override
101101
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
102102
try {
103-
String response = responseBody == null ? null : new String(responseBody, getCharset());
103+
String response = responseBody == null ? null : new String(responseBody, getCharset());
104104
onSuccess(statusCode, headers, response);
105105
} catch (UnsupportedEncodingException e) {
106106
Log.v(LOG_TAG, "String encoding failed, calling onFailure(int, Header[], String, Throwable)");
@@ -111,7 +111,7 @@ public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
111111
@Override
112112
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
113113
try {
114-
String response = responseBody == null ? null : new String(responseBody, getCharset());
114+
String response = responseBody == null ? null : new String(responseBody, getCharset());
115115
onFailure(statusCode, headers, response, error);
116116
} catch (UnsupportedEncodingException e) {
117117
Log.v(LOG_TAG, "String encoding failed, calling onFailure(int, Header[], String, Throwable)");

0 commit comments

Comments
 (0)