mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-04-24 00:00:03 +08:00
- Android: support latin1 (iso-8859-1) encoded data
- Android: some refactoring
This commit is contained in:
@@ -92,6 +92,8 @@ import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.TrustManagerFactory;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
|
||||
import android.text.TextUtils;
|
||||
|
||||
/**
|
||||
* A fluid interface for making HTTP requests using an underlying
|
||||
* {@link HttpURLConnection} (or sub-class).
|
||||
@@ -106,6 +108,11 @@ public class HttpRequest {
|
||||
*/
|
||||
public static final String CHARSET_UTF8 = "UTF-8";
|
||||
|
||||
/**
|
||||
* 'ISO-8859-1' charset name
|
||||
*/
|
||||
public static final String CHARSET_LATIN1 = "ISO-8859-1";
|
||||
|
||||
/**
|
||||
* 'application/x-www-form-urlencoded' content type header value
|
||||
*/
|
||||
@@ -2538,6 +2545,16 @@ public class HttpRequest {
|
||||
return header(HEADER_ACCEPT_CHARSET, acceptCharset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the 'Accept-Charset' header to given values
|
||||
*
|
||||
* @param acceptCharsets
|
||||
* @return this request
|
||||
*/
|
||||
public HttpRequest acceptCharset(final String[] acceptCharsets) {
|
||||
return header(HEADER_ACCEPT_CHARSET, TextUtils.join(", ", acceptCharsets));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the 'Content-Encoding' header from the response
|
||||
*
|
||||
|
||||
@@ -28,7 +28,7 @@ import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
|
||||
|
||||
abstract class CordovaHttp {
|
||||
protected static final String TAG = "CordovaHTTP";
|
||||
protected static final String CHARSET = "UTF-8";
|
||||
protected static final String[] ACCEPTED_CHARSETS = new String[] {HttpRequest.CHARSET_UTF8, HttpRequest.CHARSET_LATIN1};
|
||||
|
||||
private static AtomicBoolean sslPinning = new AtomicBoolean(false);
|
||||
private static AtomicBoolean acceptAllCerts = new AtomicBoolean(false);
|
||||
@@ -141,14 +141,13 @@ abstract class CordovaHttp {
|
||||
}
|
||||
|
||||
protected HttpRequest setupDataSerializer(HttpRequest request) throws JSONException, Exception {
|
||||
if (new String("json").equals(this.getSerializerName())) {
|
||||
if ("json".equals(this.getSerializerName())) {
|
||||
request.contentType(request.CONTENT_TYPE_JSON, request.CHARSET_UTF8);
|
||||
request.send(this.getParamsObject().toString());
|
||||
} else if (new String("utf8").equals(this.getSerializerName())) {
|
||||
} else if ("utf8".equals(this.getSerializerName())) {
|
||||
request.contentType("text/plain", request.CHARSET_UTF8);
|
||||
request.send(this.getParamsMap().get("text").toString());
|
||||
} else
|
||||
{
|
||||
} else {
|
||||
request.form(this.getParamsMap());
|
||||
}
|
||||
|
||||
@@ -227,7 +226,7 @@ abstract class CordovaHttp {
|
||||
this.setupRedirect(request);
|
||||
this.setupSecurity(request);
|
||||
request.readTimeout(this.getRequestTimeout());
|
||||
request.acceptCharset(CHARSET);
|
||||
request.acceptCharset(ACCEPTED_CHARSETS);
|
||||
request.headers(this.getHeadersMap());
|
||||
request.uncompress(true);
|
||||
}
|
||||
@@ -236,7 +235,7 @@ abstract class CordovaHttp {
|
||||
try {
|
||||
JSONObject response = new JSONObject();
|
||||
int code = request.code();
|
||||
String body = request.body(CHARSET);
|
||||
String body = request.body(request.charset());
|
||||
|
||||
response.put("status", code);
|
||||
response.put("url", request.url().toString());
|
||||
|
||||
@@ -26,41 +26,12 @@ class CordovaHttpDelete extends CordovaHttp implements Runnable {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.delete(this.getUrlString(), this.getParamsMap(), false);
|
||||
|
||||
request.readTimeout(this.getRequestTimeout());
|
||||
this.setupRedirect(request);
|
||||
this.setupSecurity(request);
|
||||
request.acceptCharset(CHARSET);
|
||||
request.headers(this.getHeadersMap());
|
||||
request.uncompress(true);
|
||||
|
||||
int code = request.code();
|
||||
String body = request.body(CHARSET);
|
||||
JSONObject response = new JSONObject();
|
||||
|
||||
this.addResponseHeaders(request, response);
|
||||
response.put("status", code);
|
||||
|
||||
if (code >= 200 && code < 300) {
|
||||
response.put("data", body);
|
||||
this.getCallbackContext().success(response);
|
||||
} else {
|
||||
response.put("error", body);
|
||||
this.getCallbackContext().error(response);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
this.respondWithError("There was an error generating the response");
|
||||
this.prepareRequest(request);
|
||||
this.returnResponseObject(request);
|
||||
} catch (HttpRequestException e) {
|
||||
if (e.getCause() instanceof UnknownHostException) {
|
||||
this.respondWithError(0, "The host could not be resolved");
|
||||
} else if (e.getCause() instanceof SocketTimeoutException) {
|
||||
this.respondWithError(1, "The request timed out");
|
||||
} else if (e.getCause() instanceof SSLHandshakeException) {
|
||||
this.respondWithError("SSL handshake failed");
|
||||
} else {
|
||||
this.respondWithError("There was an error with the request: " + e.getMessage());
|
||||
}
|
||||
this.handleHttpRequestException(e);
|
||||
} catch (Exception e) {
|
||||
this.respondWithError(-1, e.getMessage());
|
||||
this.respondWithError(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user