Skip to content

Commit 2f01d48

Browse files
author
Sefa Ilkimen
committed
- Android: support latin1 (iso-8859-1) encoded data
- Android: some refactoring
1 parent 53df68f commit 2f01d48

File tree

3 files changed

+27
-40
lines changed

3 files changed

+27
-40
lines changed

src/android/com/github/kevinsawicki/http/HttpRequest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@
9292
import javax.net.ssl.TrustManagerFactory;
9393
import javax.net.ssl.X509TrustManager;
9494

95+
import android.text.TextUtils;
96+
9597
/**
9698
* A fluid interface for making HTTP requests using an underlying
9799
* {@link HttpURLConnection} (or sub-class).
@@ -106,6 +108,11 @@ public class HttpRequest {
106108
*/
107109
public static final String CHARSET_UTF8 = "UTF-8";
108110

111+
/**
112+
* 'ISO-8859-1' charset name
113+
*/
114+
public static final String CHARSET_LATIN1 = "ISO-8859-1";
115+
109116
/**
110117
* 'application/x-www-form-urlencoded' content type header value
111118
*/
@@ -2538,6 +2545,16 @@ public HttpRequest acceptCharset(final String acceptCharset) {
25382545
return header(HEADER_ACCEPT_CHARSET, acceptCharset);
25392546
}
25402547

2548+
/**
2549+
* Set the 'Accept-Charset' header to given values
2550+
*
2551+
* @param acceptCharsets
2552+
* @return this request
2553+
*/
2554+
public HttpRequest acceptCharset(final String[] acceptCharsets) {
2555+
return header(HEADER_ACCEPT_CHARSET, TextUtils.join(", ", acceptCharsets));
2556+
}
2557+
25412558
/**
25422559
* Get the 'Content-Encoding' header from the response
25432560
*

src/android/com/synconset/cordovahttp/CordovaHttp.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
abstract class CordovaHttp {
3030
protected static final String TAG = "CordovaHTTP";
31-
protected static final String CHARSET = "UTF-8";
31+
protected static final String[] ACCEPTED_CHARSETS = new String[] {HttpRequest.CHARSET_UTF8, HttpRequest.CHARSET_LATIN1};
3232

3333
private static AtomicBoolean sslPinning = new AtomicBoolean(false);
3434
private static AtomicBoolean acceptAllCerts = new AtomicBoolean(false);
@@ -141,14 +141,13 @@ protected HttpRequest setupRedirect(HttpRequest request) {
141141
}
142142

143143
protected HttpRequest setupDataSerializer(HttpRequest request) throws JSONException, Exception {
144-
if (new String("json").equals(this.getSerializerName())) {
144+
if ("json".equals(this.getSerializerName())) {
145145
request.contentType(request.CONTENT_TYPE_JSON, request.CHARSET_UTF8);
146146
request.send(this.getParamsObject().toString());
147-
} else if (new String("utf8").equals(this.getSerializerName())) {
147+
} else if ("utf8".equals(this.getSerializerName())) {
148148
request.contentType("text/plain", request.CHARSET_UTF8);
149149
request.send(this.getParamsMap().get("text").toString());
150-
} else
151-
{
150+
} else {
152151
request.form(this.getParamsMap());
153152
}
154153

@@ -227,7 +226,7 @@ protected void prepareRequest(HttpRequest request) throws HttpRequestException,
227226
this.setupRedirect(request);
228227
this.setupSecurity(request);
229228
request.readTimeout(this.getRequestTimeout());
230-
request.acceptCharset(CHARSET);
229+
request.acceptCharset(ACCEPTED_CHARSETS);
231230
request.headers(this.getHeadersMap());
232231
request.uncompress(true);
233232
}
@@ -236,7 +235,7 @@ protected void returnResponseObject(HttpRequest request) throws HttpRequestExcep
236235
try {
237236
JSONObject response = new JSONObject();
238237
int code = request.code();
239-
String body = request.body(CHARSET);
238+
String body = request.body(request.charset());
240239

241240
response.put("status", code);
242241
response.put("url", request.url().toString());

src/android/com/synconset/cordovahttp/CordovaHttpDelete.java

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,41 +26,12 @@ public void run() {
2626
try {
2727
HttpRequest request = HttpRequest.delete(this.getUrlString(), this.getParamsMap(), false);
2828

29-
request.readTimeout(this.getRequestTimeout());
30-
this.setupRedirect(request);
31-
this.setupSecurity(request);
32-
request.acceptCharset(CHARSET);
33-
request.headers(this.getHeadersMap());
34-
request.uncompress(true);
35-
36-
int code = request.code();
37-
String body = request.body(CHARSET);
38-
JSONObject response = new JSONObject();
39-
40-
this.addResponseHeaders(request, response);
41-
response.put("status", code);
42-
43-
if (code >= 200 && code < 300) {
44-
response.put("data", body);
45-
this.getCallbackContext().success(response);
46-
} else {
47-
response.put("error", body);
48-
this.getCallbackContext().error(response);
49-
}
50-
} catch (JSONException e) {
51-
this.respondWithError("There was an error generating the response");
29+
this.prepareRequest(request);
30+
this.returnResponseObject(request);
5231
} catch (HttpRequestException e) {
53-
if (e.getCause() instanceof UnknownHostException) {
54-
this.respondWithError(0, "The host could not be resolved");
55-
} else if (e.getCause() instanceof SocketTimeoutException) {
56-
this.respondWithError(1, "The request timed out");
57-
} else if (e.getCause() instanceof SSLHandshakeException) {
58-
this.respondWithError("SSL handshake failed");
59-
} else {
60-
this.respondWithError("There was an error with the request: " + e.getMessage());
61-
}
32+
this.handleHttpRequestException(e);
6233
} catch (Exception e) {
63-
this.respondWithError(-1, e.getMessage());
34+
this.respondWithError(e.getMessage());
6435
}
6536
}
6637
}

0 commit comments

Comments
 (0)