mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-04-24 00:00:03 +08:00
WIP: re-implement Upload and Download Runnables
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
package com.silkimen.cordovahttp;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
|
||||
import com.silkimen.http.HttpBodyDecoder;
|
||||
import com.silkimen.http.HttpRequest;
|
||||
import com.silkimen.http.HttpRequest.HttpRequestException;
|
||||
import com.silkimen.http.JsonUtils;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.file.FileUtils;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
class CordovaHttpDownload implements Runnable {
|
||||
private static final String TAG = "Cordova-Plugin-HTTP";
|
||||
|
||||
private String url;
|
||||
private JSONObject params;
|
||||
private JSONObject headers;
|
||||
private String filePath;
|
||||
private int timeout;
|
||||
private CallbackContext callbackContext;
|
||||
|
||||
public CordovaHttpDownload(String url, JSONObject params, JSONObject headers, String filePath, int timeout,
|
||||
CallbackContext callbackContext) {
|
||||
|
||||
this.url = url;
|
||||
this.params = params;
|
||||
this.headers = headers;
|
||||
this.filePath = filePath;
|
||||
this.timeout = timeout;
|
||||
this.callbackContext = callbackContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
CordovaHttpResponse response = new CordovaHttpResponse();
|
||||
|
||||
try {
|
||||
String processedUrl = HttpRequest.encode(HttpRequest.append(this.url, JsonUtils.getObjectMap(this.params)));
|
||||
|
||||
HttpRequest request = new HttpRequest(processedUrl, "GET")
|
||||
.followRedirects(true /* @TODO */)
|
||||
.readTimeout(this.timeout)
|
||||
.acceptCharset("UTF-8")
|
||||
.uncompress(true)
|
||||
.headers(JsonUtils.getStringMap(this.headers));
|
||||
|
||||
response.setStatus(request.code());
|
||||
response.setUrl(request.url().toString());
|
||||
response.setHeaders(request.headers());
|
||||
|
||||
if (request.code() >= 200 && request.code() < 300) {
|
||||
File file = new File(new URI(filePath));
|
||||
|
||||
request.receive(file);
|
||||
response.setFileEntry(FileUtils.getFilePlugin().getEntryForFile(file));
|
||||
} else {
|
||||
response.setErrorMessage("There was an error downloading the file");
|
||||
}
|
||||
} catch (HttpRequestException e) {
|
||||
if (e.getCause() instanceof SSLHandshakeException) {
|
||||
response.setStatus(-2);
|
||||
response.setErrorMessage("SSL handshake failed: " + e.getMessage());
|
||||
Log.w(TAG, "SSL handshake failed", e);
|
||||
} else if (e.getCause() instanceof UnknownHostException) {
|
||||
response.setStatus(-3);
|
||||
response.setErrorMessage("Host could not be resolved: " + e.getMessage());
|
||||
Log.w(TAG, "Host could not be resolved", e);
|
||||
} else if (e.getCause() instanceof SocketTimeoutException) {
|
||||
response.setStatus(-4);
|
||||
response.setErrorMessage("Request timed out: " + e.getMessage());
|
||||
Log.w(TAG, "Request timed out", e);
|
||||
} else {
|
||||
response.setStatus(-1);
|
||||
response.setErrorMessage("There was an error with the request: " + e.getCause().getMessage());
|
||||
Log.w(TAG, "Generic request error", e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
response.setStatus(-1);
|
||||
response.setErrorMessage(e.getMessage());
|
||||
Log.e(TAG, "An unexpected error occured", e);
|
||||
}
|
||||
|
||||
try {
|
||||
if (response.hasFailed()) {
|
||||
this.callbackContext.error(response.toJSON());
|
||||
} else {
|
||||
this.callbackContext.success(response.toJSON());
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "An unexpected error occured while processing HTTP response", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
package com.silkimen.cordovahttp;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.KeyStore;
|
||||
import java.security.KeyStore.TrustedCertificateEntry;
|
||||
import java.security.cert.Certificate;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaInterface;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.CordovaWebView;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.util.Log;
|
||||
import android.content.res.AssetManager;
|
||||
|
||||
public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
private static final String TAG = "Cordova-Plugin-HTTP";
|
||||
|
||||
private static boolean followRedirects = true;
|
||||
|
||||
@Override
|
||||
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
|
||||
super.initialize(cordova, webView);
|
||||
|
||||
try {
|
||||
// HttpRequest.clearCerts();
|
||||
this.pinSSLCertsFromCAStore();
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "An error occured while loading system's CA certificates", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext)
|
||||
throws JSONException {
|
||||
|
||||
if (action == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (action) {
|
||||
case "get":
|
||||
return this.executeHttpRequestWithParams(action, args, callbackContext);
|
||||
case "post":
|
||||
return this.executeHttpRequestWithData(action, args, callbackContext);
|
||||
case "put":
|
||||
return this.executeHttpRequestWithData(action, args, callbackContext);
|
||||
case "patch":
|
||||
return this.executeHttpRequestWithData(action, args, callbackContext);
|
||||
case "head":
|
||||
return this.executeHttpRequestWithParams(action, args, callbackContext);
|
||||
case "delete":
|
||||
return this.executeHttpRequestWithParams(action, args, callbackContext);
|
||||
case "uploadFile":
|
||||
return this.uploadFile(args, callbackContext);
|
||||
case "downloadFile":
|
||||
return this.downloadFile(args, callbackContext);
|
||||
case "setSSLCertMode":
|
||||
return this.setSSLCertMode(args, callbackContext);
|
||||
case "disableRedirect":
|
||||
return this.disableRedirect(args, callbackContext);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean executeHttpRequestWithParams(final String method, final JSONArray args,
|
||||
final CallbackContext callbackContext) throws JSONException {
|
||||
|
||||
String url = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
int timeout = args.getInt(3) * 1000;
|
||||
|
||||
CordovaHttpRequest get = new CordovaHttpRequest(method.toUpperCase(), url, params, headers, timeout,
|
||||
callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(get);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean executeHttpRequestWithData(final String method, final JSONArray args,
|
||||
final CallbackContext callbackContext) throws JSONException {
|
||||
|
||||
String url = args.getString(0);
|
||||
Object data = args.get(1);
|
||||
String serializer = args.getString(2);
|
||||
JSONObject headers = args.getJSONObject(3);
|
||||
int timeout = args.getInt(4) * 1000;
|
||||
|
||||
CordovaHttpRequest request = new CordovaHttpRequest(method.toUpperCase(), url, serializer, data, headers, timeout,
|
||||
callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(request);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean uploadFile(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
||||
String url = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
String filePath = args.getString(3);
|
||||
String uploadName = args.getString(4);
|
||||
int timeout = args.getInt(5) * 1000;
|
||||
|
||||
CordovaHttpUpload upload = new CordovaHttpUpload(url, params, headers,
|
||||
filePath, uploadName, timeout, callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(upload);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean downloadFile(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
||||
String url = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
String filePath = args.getString(3);
|
||||
int timeout = args.getInt(4) * 1000;
|
||||
|
||||
CordovaHttpDownload download = new CordovaHttpDownload(url, params, headers, filePath, timeout, callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(download);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean setSSLCertMode(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
||||
String mode = args.getString(0);
|
||||
|
||||
// HttpRequest.clearCerts();
|
||||
|
||||
if (mode.equals("legacy")) {
|
||||
// HttpRequest.setSSLCertMode(HttpRequest.CERT_MODE_DEFAULT);
|
||||
callbackContext.success();
|
||||
} else if (mode.equals("nocheck")) {
|
||||
// HttpRequest.setSSLCertMode(HttpRequest.CERT_MODE_TRUSTALL);
|
||||
callbackContext.success();
|
||||
} else if (mode.equals("pinned")) {
|
||||
try {
|
||||
this.loadSSLCertsFromBundle();
|
||||
// HttpRequest.setSSLCertMode(HttpRequest.CERT_MODE_PINNED);
|
||||
callbackContext.success();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error("There was an error setting up ssl pinning");
|
||||
}
|
||||
} else if (mode.equals("default")) {
|
||||
try {
|
||||
this.pinSSLCertsFromCAStore();
|
||||
callbackContext.success();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error("There was an error loading system's CA certificates");
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean disableRedirect(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
||||
followRedirects = !args.getBoolean(0);
|
||||
|
||||
callbackContext.success();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void pinSSLCertsFromCAStore() throws GeneralSecurityException, IOException {
|
||||
this.loadSSLCertsFromKeyStore("AndroidCAStore");
|
||||
// HttpRequest.setSSLCertMode(HttpRequest.CERT_MODE_PINNED);
|
||||
}
|
||||
|
||||
private void loadSSLCertsFromKeyStore(String storeType) throws GeneralSecurityException, IOException {
|
||||
KeyStore ks = KeyStore.getInstance(storeType);
|
||||
ks.load(null);
|
||||
Enumeration<String> aliases = ks.aliases();
|
||||
|
||||
while (aliases.hasMoreElements()) {
|
||||
String alias = aliases.nextElement();
|
||||
TrustedCertificateEntry certEntry = (TrustedCertificateEntry) ks.getEntry(alias, null);
|
||||
Certificate cert = certEntry.getTrustedCertificate();
|
||||
// HttpRequest.addCert(cert);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadSSLCertsFromBundle() throws GeneralSecurityException, IOException {
|
||||
AssetManager assetManager = cordova.getActivity().getAssets();
|
||||
String[] files = assetManager.list("www/certificates");
|
||||
ArrayList<String> cerFiles = new ArrayList<String>();
|
||||
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
int index = files[i].lastIndexOf('.');
|
||||
if (index != -1) {
|
||||
if (files[i].substring(index).equals(".cer")) {
|
||||
cerFiles.add("www/certificates/" + files[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < cerFiles.size(); i++) {
|
||||
InputStream in = cordova.getActivity().getAssets().open(cerFiles.get(i));
|
||||
InputStream caInput = new BufferedInputStream(in);
|
||||
// HttpRequest.addCert(caInput);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.silkimen.cordovahttp;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
@@ -10,16 +12,18 @@ import javax.net.ssl.SSLHandshakeException;
|
||||
import com.silkimen.http.HttpBodyDecoder;
|
||||
import com.silkimen.http.HttpRequest;
|
||||
import com.silkimen.http.HttpRequest.HttpRequestException;
|
||||
import com.silkimen.http.HttpResponse;
|
||||
import com.silkimen.http.JsonUtils;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
public class CordovaHttpRequest implements Runnable {
|
||||
private static final String TAG = "Cordova-Plugin-HTTP";
|
||||
|
||||
private String method;
|
||||
private String url;
|
||||
private String serializer = "none";
|
||||
@@ -54,7 +58,7 @@ public class CordovaHttpRequest implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
HttpResponse response = new HttpResponse();
|
||||
CordovaHttpResponse response = new CordovaHttpResponse();
|
||||
|
||||
try {
|
||||
String processedUrl = HttpRequest.encode(HttpRequest.append(this.url, JsonUtils.getObjectMap(this.params)));
|
||||
@@ -82,33 +86,31 @@ public class CordovaHttpRequest implements Runnable {
|
||||
|
||||
if (request.code() >= 200 && request.code() < 300) {
|
||||
response.setBody(decodedBody);
|
||||
this.callbackContext.success(response.toJSON());
|
||||
} else {
|
||||
response.setErrorMessage(decodedBody);
|
||||
this.callbackContext.error(response.toJSON());
|
||||
}
|
||||
} catch (HttpRequestException e) {
|
||||
if (e.getCause() instanceof SSLHandshakeException) {
|
||||
response.setStatus(-2);
|
||||
response.setErrorMessage("SSL handshake failed: " + e.getMessage());
|
||||
Log.w("Cordova-Plugin-HTTP", "SSL handshake failed", e);
|
||||
Log.w(TAG, "SSL handshake failed", e);
|
||||
} else if (e.getCause() instanceof UnknownHostException) {
|
||||
response.setStatus(-3);
|
||||
response.setErrorMessage("Host could not be resolved: " + e.getMessage());
|
||||
Log.w("Cordova-Plugin-HTTP", "Host could not be resolved", e);
|
||||
Log.w(TAG, "Host could not be resolved", e);
|
||||
} else if (e.getCause() instanceof SocketTimeoutException) {
|
||||
response.setStatus(-4);
|
||||
response.setErrorMessage("Request timed out: " + e.getMessage());
|
||||
Log.w("Cordova-Plugin-HTTP", "Request timed out", e);
|
||||
Log.w(TAG, "Request timed out", e);
|
||||
} else {
|
||||
response.setStatus(-1);
|
||||
response.setErrorMessage("There was an error with the request: " + e.getCause().getMessage());
|
||||
Log.w("Cordova-Plugin-HTTP", "Generic request error", e);
|
||||
Log.w(TAG, "Generic request error", e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
response.setStatus(-1);
|
||||
response.setErrorMessage(e.getMessage());
|
||||
Log.e("Cordova-Plugin-HTTP", "An unexpected error occured", e);
|
||||
Log.e(TAG, "An unexpected error occured", e);
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -118,7 +120,7 @@ public class CordovaHttpRequest implements Runnable {
|
||||
this.callbackContext.success(response.toJSON());
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
Log.e("Cordova-Plugin-HTTP", "An unexpected error occured while processing HTTP response", e);
|
||||
Log.e(TAG, "An unexpected error occured while processing HTTP response", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+16
-6
@@ -1,4 +1,4 @@
|
||||
package com.silkimen.http;
|
||||
package com.silkimen.cordovahttp;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -10,12 +10,14 @@ import org.json.JSONObject;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
public class HttpResponse {
|
||||
public class CordovaHttpResponse {
|
||||
private int status;
|
||||
private String url;
|
||||
private Map<String, List<String>> headers;
|
||||
private String body;
|
||||
private boolean failed;
|
||||
private JSONObject fileEntry;
|
||||
private boolean hasFailed;
|
||||
private boolean isFileOperation;
|
||||
private String error;
|
||||
|
||||
public void setStatus(int status) {
|
||||
@@ -34,13 +36,18 @@ public class HttpResponse {
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public void setFileEntry(JSONObject entry) {
|
||||
this.isFileOperation = true;
|
||||
this.fileEntry = entry;
|
||||
}
|
||||
|
||||
public void setErrorMessage(String message) {
|
||||
this.failed = true;
|
||||
this.hasFailed = true;
|
||||
this.error = message;
|
||||
}
|
||||
|
||||
public boolean hasFailed() {
|
||||
return this.failed;
|
||||
return this.hasFailed;
|
||||
}
|
||||
|
||||
public JSONObject toJSON() throws JSONException {
|
||||
@@ -49,8 +56,11 @@ public class HttpResponse {
|
||||
json.put("status", this.status);
|
||||
json.put("url", this.url);
|
||||
|
||||
if (this.failed) {
|
||||
if (this.hasFailed) {
|
||||
json.put("error", this.error);
|
||||
} else if (this.isFileOperation) {
|
||||
json.put("headers", new JSONObject(getFilteredHeaders()));
|
||||
json.put("file", this.fileEntry);
|
||||
} else {
|
||||
json.put("headers", new JSONObject(getFilteredHeaders()));
|
||||
json.put("data", this.body);
|
||||
@@ -0,0 +1,128 @@
|
||||
package com.silkimen.cordovahttp;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
|
||||
import com.silkimen.http.HttpBodyDecoder;
|
||||
import com.silkimen.http.HttpRequest;
|
||||
import com.silkimen.http.HttpRequest.HttpRequestException;
|
||||
import com.silkimen.http.JsonUtils;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.webkit.MimeTypeMap;
|
||||
import android.util.Log;
|
||||
|
||||
class CordovaHttpUpload implements Runnable {
|
||||
private static final String TAG = "Cordova-Plugin-HTTP";
|
||||
|
||||
private String url;
|
||||
private JSONObject params;
|
||||
private JSONObject headers;
|
||||
private String filePath;
|
||||
private String uploadName;
|
||||
private int timeout;
|
||||
private CallbackContext callbackContext;
|
||||
|
||||
public CordovaHttpUpload(String url, JSONObject params, JSONObject headers, String filePath, String uploadName,
|
||||
int timeout, CallbackContext callbackContext) {
|
||||
|
||||
this.url = url;
|
||||
this.params = params;
|
||||
this.headers = headers;
|
||||
this.filePath = filePath;
|
||||
this.uploadName = uploadName;
|
||||
this.timeout = timeout;
|
||||
this.callbackContext = callbackContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
CordovaHttpResponse response = new CordovaHttpResponse();
|
||||
|
||||
try {
|
||||
String processedUrl = HttpRequest.encode(HttpRequest.append(this.url, JsonUtils.getObjectMap(this.params)));
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
|
||||
HttpRequest request = new HttpRequest(processedUrl, "POST")
|
||||
.followRedirects(true /* @TODO */)
|
||||
.readTimeout(this.timeout)
|
||||
.acceptCharset("UTF-8")
|
||||
.uncompress(true)
|
||||
.headers(JsonUtils.getStringMap(this.headers));
|
||||
|
||||
int filenameIndex = filePath.lastIndexOf('/');
|
||||
String filename = filePath.substring(filenameIndex + 1);
|
||||
|
||||
int extIndex = filePath.lastIndexOf('.');
|
||||
String ext = filePath.substring(extIndex + 1);
|
||||
|
||||
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
|
||||
String mimeType = mimeTypeMap.getMimeTypeFromExtension(ext);
|
||||
|
||||
request.part(this.uploadName, filename, mimeType, new File(new URI(this.filePath)))
|
||||
.receive(outputStream);
|
||||
|
||||
ByteBuffer rawOutput = ByteBuffer.wrap(outputStream.toByteArray());
|
||||
String decodedBody = HttpBodyDecoder.decodeBody(rawOutput, request.charset());
|
||||
|
||||
response.setStatus(request.code());
|
||||
response.setUrl(request.url().toString());
|
||||
response.setHeaders(request.headers());
|
||||
|
||||
if (request.code() >= 200 && request.code() < 300) {
|
||||
response.setBody(decodedBody);
|
||||
} else {
|
||||
response.setErrorMessage(decodedBody);
|
||||
}
|
||||
} catch (HttpRequestException e) {
|
||||
if (e.getCause() instanceof SSLHandshakeException) {
|
||||
response.setStatus(-2);
|
||||
response.setErrorMessage("SSL handshake failed: " + e.getMessage());
|
||||
Log.w(TAG, "SSL handshake failed", e);
|
||||
} else if (e.getCause() instanceof UnknownHostException) {
|
||||
response.setStatus(-3);
|
||||
response.setErrorMessage("Host could not be resolved: " + e.getMessage());
|
||||
Log.w(TAG, "Host could not be resolved", e);
|
||||
} else if (e.getCause() instanceof SocketTimeoutException) {
|
||||
response.setStatus(-4);
|
||||
response.setErrorMessage("Request timed out: " + e.getMessage());
|
||||
Log.w(TAG, "Request timed out", e);
|
||||
} else {
|
||||
response.setStatus(-1);
|
||||
response.setErrorMessage("There was an error with the request: " + e.getCause().getMessage());
|
||||
Log.w(TAG, "Generic request error", e);
|
||||
}
|
||||
} catch (URISyntaxException e) {
|
||||
response.setStatus(-1);
|
||||
response.setErrorMessage("An error occured while loading file");
|
||||
Log.e(TAG, "An error occured while loading file", e);
|
||||
} catch (Exception e) {
|
||||
response.setStatus(-1);
|
||||
response.setErrorMessage(e.getMessage());
|
||||
Log.e(TAG, "An unexpected error occured", e);
|
||||
}
|
||||
|
||||
try {
|
||||
if (response.hasFailed()) {
|
||||
this.callbackContext.error(response.toJSON());
|
||||
} else {
|
||||
this.callbackContext.success(response.toJSON());
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "An unexpected error occured while processing HTTP response", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user