Skip to content

Commit b3af51b

Browse files
author
Noor Dawod
committed
Group up strings into classes and reuse them.
1 parent ffea1a0 commit b3af51b

File tree

8 files changed

+61
-26
lines changed

8 files changed

+61
-26
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,20 @@
113113
*/
114114
public class AsyncHttpClient {
115115

116+
public static final String LOG_TAG = "AsyncHttpClient";
117+
118+
public static final String HEADER_CONTENT_TYPE = "Content-Type";
119+
public static final String HEADER_CONTENT_RANGE = "Content-Range";
120+
public static final String HEADER_CONTENT_ENCODING = "Content-Encoding";
121+
public static final String HEADER_CONTENT_DISPOSITION = "Content-Disposition";
122+
public static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
123+
public static final String ENCODING_GZIP = "gzip";
124+
116125
public static final int DEFAULT_MAX_CONNECTIONS = 10;
117126
public static final int DEFAULT_SOCKET_TIMEOUT = 10 * 1000;
118127
public static final int DEFAULT_MAX_RETRIES = 5;
119128
public static final int DEFAULT_RETRY_SLEEP_TIME_MILLIS = 1500;
120129
public static final int DEFAULT_SOCKET_BUFFER_SIZE = 8192;
121-
public static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
122-
public static final String ENCODING_GZIP = "gzip";
123-
public static final String LOG_TAG = "AsyncHttpClient";
124130

125131
private int maxConnections = DEFAULT_MAX_CONNECTIONS;
126132
private int timeout = DEFAULT_SOCKET_TIMEOUT;
@@ -1090,7 +1096,7 @@ protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpCo
10901096
}
10911097

10921098
if (contentType != null) {
1093-
uriRequest.setHeader("Content-Type", contentType);
1099+
uriRequest.setHeader(HEADER_CONTENT_TYPE, contentType);
10941100
}
10951101

10961102
responseHandler.setRequestHeaders(uriRequest.getAllHeaders());

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ public abstract class BinaryHttpResponseHandler extends AsyncHttpResponseHandler
5353

5454
private static final String LOG_TAG = "BinaryHttpResponseHandler";
5555

56-
private String[] mAllowedContentTypes = new String[]{
57-
"image/jpeg",
58-
"image/png",
59-
"image/gif",
60-
"application/octet-stream"
56+
private String[] mAllowedContentTypes = new String[] {
57+
RequestParams.APPLICATION_OCTET_STREAM,
58+
"image/jpeg",
59+
"image/png",
60+
"image/gif"
6161
};
6262

6363
/**
@@ -100,10 +100,18 @@ public BinaryHttpResponseHandler(String[] allowedContentTypes) {
100100
@Override
101101
public final void sendResponseMessage(HttpResponse response) throws IOException {
102102
StatusLine status = response.getStatusLine();
103-
Header[] contentTypeHeaders = response.getHeaders("Content-Type");
103+
Header[] contentTypeHeaders = response.getHeaders(AsyncHttpClient.HEADER_CONTENT_TYPE);
104104
if (contentTypeHeaders.length != 1) {
105105
//malformed/ambiguous HTTP Header, ABORT!
106-
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), null, new HttpResponseException(status.getStatusCode(), "None, or more than one, Content-Type Header found!"));
106+
sendFailureMessage(
107+
status.getStatusCode(),
108+
response.getAllHeaders(),
109+
null,
110+
new HttpResponseException(
111+
status.getStatusCode(),
112+
"None, or more than one, Content-Type Header found!"
113+
)
114+
);
107115
return;
108116
}
109117
Header contentTypeHeader = contentTypeHeaders[0];
@@ -119,7 +127,15 @@ public final void sendResponseMessage(HttpResponse response) throws IOException
119127
}
120128
if (!foundAllowedContentType) {
121129
//Content-Type not in allowed list, ABORT!
122-
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), null, new HttpResponseException(status.getStatusCode(), "Content-Type not allowed!"));
130+
sendFailureMessage(
131+
status.getStatusCode(),
132+
response.getAllHeaders(),
133+
null,
134+
new HttpResponseException(
135+
status.getStatusCode(),
136+
"Content-Type not allowed!"
137+
)
138+
);
123139
return;
124140
}
125141
super.sendResponseMessage(response);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ public class JsonHttpResponseHandler extends TextHttpResponseHandler {
3939
private static final String LOG_TAG = "JsonHttpResponseHandler";
4040

4141
/**
42-
* Creates new JsonHttpResponseHandler, with Json String encoding UTF-8
42+
* Creates new JsonHttpResponseHandler, with JSON String encoding UTF-8
4343
*/
4444
public JsonHttpResponseHandler() {
4545
super(DEFAULT_CHARSET);
4646
}
4747

4848
/**
49-
* Creates new JsonHttpRespnseHandler with given Json String encoding
49+
* Creates new JsonHttpRespnseHandler with given JSON String encoding
5050
*
5151
* @param encoding String encoding to be used when parsing JSON
5252
*/
@@ -208,7 +208,7 @@ protected Object parseResponse(byte[] responseBody) throws JSONException {
208208
if (null == responseBody)
209209
return null;
210210
Object result = null;
211-
//trim the string to prevent start with blank, and test if the string is valid JSON, because the parser don't do this :(. If Json is not valid this will return null
211+
//trim the string to prevent start with blank, and test if the string is valid JSON, because the parser don't do this :(. If JSON is not valid this will return null
212212
String jsonString = getResponseString(responseBody, getCharset());
213213
if (jsonString != null) {
214214
jsonString = jsonString.trim();

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,14 @@ public class JsonStreamerEntity implements HttpEntity {
6565
private static final byte[] STREAM_ELAPSED = escape("_elapsed");
6666

6767
private static final Header HEADER_JSON_CONTENT =
68-
new BasicHeader("Content-Type", "application/json");
68+
new BasicHeader(
69+
AsyncHttpClient.HEADER_CONTENT_TYPE,
70+
RequestParams.APPLICATION_JSON);
71+
6972
private static final Header HEADER_GZIP_ENCODING =
70-
new BasicHeader("Content-Encoding", "gzip");
73+
new BasicHeader(
74+
AsyncHttpClient.HEADER_CONTENT_ENCODING,
75+
AsyncHttpClient.ENCODING_GZIP);
7176

7277
// JSON data and associated meta-data to be uploaded.
7378
private final Map<String, Object> jsonParams = new HashMap<String, Object>();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ public void sendResponseMessage(HttpResponse response) throws IOException {
6262
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), null, new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()));
6363
} else {
6464
if (!Thread.currentThread().isInterrupted()) {
65-
Header header = response.getFirstHeader("Content-Range");
65+
Header header = response.getFirstHeader(AsyncHttpClient.HEADER_CONTENT_RANGE);
6666
if (header == null) {
6767
append = false;
6868
current = 0;
6969
} else
70-
Log.v(LOG_TAG, "Content-Range: " + header.getValue());
70+
Log.v(LOG_TAG, AsyncHttpClient.HEADER_CONTENT_RANGE + ": " + header.getValue());
7171
sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), getResponseData(response.getEntity()));
7272
}
7373
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ public class RequestParams {
9292
public final static String APPLICATION_OCTET_STREAM =
9393
"application/octet-stream";
9494

95+
public final static String APPLICATION_JSON =
96+
"application/json";
97+
9598
protected final static String LOG_TAG = "RequestParams";
9699
protected boolean isRepeatable;
97100
protected boolean useJsonStreamer;

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,21 @@ private String normalizeContentType(String type) {
146146
}
147147

148148
private byte[] createContentType(String type) {
149-
String result = "Content-Type: " + normalizeContentType(type) + STR_CR_LF;
149+
String result = AsyncHttpClient.HEADER_CONTENT_TYPE + ": " + normalizeContentType(type) + STR_CR_LF;
150150
return result.getBytes();
151151
}
152152

153153
private byte[] createContentDisposition(String key) {
154-
return ("Content-Disposition: form-data; name=\"" + key + "\"" + STR_CR_LF)
155-
.getBytes();
154+
return (
155+
AsyncHttpClient.HEADER_CONTENT_DISPOSITION +
156+
": form-data; name=\"" + key + "\"" + STR_CR_LF).getBytes();
156157
}
157158

158159
private byte[] createContentDisposition(String key, String fileName) {
159-
return ("Content-Disposition: form-data; name=\"" + key + "\"; filename=\"" + fileName + "\"" + STR_CR_LF)
160-
.getBytes();
160+
return (
161+
AsyncHttpClient.HEADER_CONTENT_DISPOSITION +
162+
": form-data; name=\"" + key + "\"" +
163+
"; filename=\"" + fileName + "\"" + STR_CR_LF).getBytes();
161164
}
162165

163166
private void updateProgress(int count) {
@@ -232,7 +235,9 @@ public long getContentLength() {
232235

233236
@Override
234237
public Header getContentType() {
235-
return new BasicHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
238+
return new BasicHeader(
239+
AsyncHttpClient.HEADER_CONTENT_TYPE,
240+
"multipart/form-data; boundary=" + boundary);
236241
}
237242

238243
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected RequestHandle sendRequest(DefaultHttpClient client,
8484
String contentType, ResponseHandlerInterface responseHandler,
8585
Context context) {
8686
if (contentType != null) {
87-
uriRequest.addHeader("Content-Type", contentType);
87+
uriRequest.addHeader(AsyncHttpClient.HEADER_CONTENT_TYPE, contentType);
8888
}
8989

9090
responseHandler.setUseSynchronousMode(true);

0 commit comments

Comments
 (0)