Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit b0306cc

Browse files
committed
Add #29 Android implementation
1 parent e37f378 commit b0306cc

File tree

4 files changed

+238
-174
lines changed

4 files changed

+238
-174
lines changed

src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java

Lines changed: 4 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
import com.loopj.android.http.AsyncHttpClient;
2020
import com.loopj.android.http.AsyncHttpResponseHandler;
2121
import com.loopj.android.http.Base64;
22+
import com.loopj.android.http.MySSLSocketFactory;
2223
import com.loopj.android.http.RequestParams;
2324

2425
import java.io.ByteArrayOutputStream;
2526
import java.io.File;
27+
import java.security.KeyStore;
2628
import java.util.HashMap;
2729
import java.util.Map;
2830

@@ -158,183 +160,12 @@ public void readStream(String path, String encoding, int bufferSize) {
158160

159161
@ReactMethod
160162
public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
161-
162-
RNFetchBlobConfig config = new RNFetchBlobConfig(options);
163-
164-
// use download manager instead of default HTTP implementation
165-
if(config.addAndroidDownloads != null && config.addAndroidDownloads.hasKey("useDownloadManager")) {
166-
167-
if(config.addAndroidDownloads.getBoolean("useDownloadManager")) {
168-
Uri uri = Uri.parse(url);
169-
DownloadManager.Request req = new DownloadManager.Request(uri);
170-
if(config.path != null) {
171-
Uri dest = null;
172-
dest = Uri.parse(config.path);
173-
req.setDestinationUri(dest);
174-
}
175-
// set headers
176-
ReadableMapKeySetIterator it = headers.keySetIterator();
177-
while (it.hasNextKey()) {
178-
String key = it.nextKey();
179-
req.addRequestHeader(key, headers.getString(key));
180-
}
181-
DownloadManager dm = (DownloadManager) this.getReactApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE);
182-
dm.enqueue(req);
183-
return;
184-
}
185-
186-
}
187-
188-
try {
189-
AsyncHttpClient req = new AsyncHttpClient();
190-
191-
AbstractHttpEntity entity = null;
192-
193-
// set headers
194-
ReadableMapKeySetIterator it = headers.keySetIterator();
195-
while (it.hasNextKey()) {
196-
String key = it.nextKey();
197-
req.addHeader(key, headers.getString(key));
198-
}
199-
200-
// set body for POST and PUT
201-
if(body != null && method.equalsIgnoreCase("post") || method.equalsIgnoreCase("put")) {
202-
203-
byte [] blob;
204-
// upload from storage
205-
if(body.startsWith(filePathPrefix)) {
206-
String filePath = body.substring(filePathPrefix.length());
207-
entity = new FileEntity(new File(filePath));
208-
}
209-
else {
210-
blob = Base64.decode(body, 0);
211-
entity = new ByteArrayEntity(blob);
212-
}
213-
entity.setContentType(headers.getString("Content-Type"));
214-
}
215-
216-
AsyncHttpResponseHandler handler;
217-
218-
// create handler
219-
if(config.fileCache || config.path != null) {
220-
handler = new RNFetchBlobFileHandler(this.getReactApplicationContext(), taskId, config, callback);
221-
// if path format invalid, throw error
222-
if (!((RNFetchBlobFileHandler)handler).isValid) {
223-
callback.invoke("RNFetchBlob fetch error, configuration path `"+ config.path +"` is not a valid path.");
224-
return;
225-
}
226-
}
227-
else
228-
handler = new RNFetchBlobBinaryHandler(this.getReactApplicationContext(), taskId, callback);
229-
230-
// send request
231-
switch(method.toLowerCase()) {
232-
case "get" :
233-
req.get(url, handler);
234-
break;
235-
case "post" :
236-
req.post(this.getReactApplicationContext(), url, entity, "octet-stream", handler);
237-
break;
238-
case "put" :
239-
req.put(this.getReactApplicationContext(), url, entity, "octet-stream",handler);
240-
break;
241-
case "delete" :
242-
req.delete(url, handler);
243-
break;
244-
}
245-
} catch(Exception error) {
246-
callback.invoke( "RNFetchBlob serialize request data failed: " + error.getMessage() + error.getCause());
247-
}
248-
163+
new RNFetchBlobReq(this.getReactApplicationContext(), options, taskId, method, url, headers, body, callback).run();
249164
}
250165

251166
@ReactMethod
252167
public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) {
253-
254-
RNFetchBlobConfig config = new RNFetchBlobConfig(options);
255-
try {
256-
257-
AsyncHttpClient req = new AsyncHttpClient();
258-
259-
HttpEntity entity = null;
260-
261-
// set headers
262-
if(headers != null) {
263-
ReadableMapKeySetIterator it = headers.keySetIterator();
264-
while (it.hasNextKey()) {
265-
String key = it.nextKey();
266-
req.addHeader(key, headers.getString(key));
267-
}
268-
}
269-
270-
// set body for POST and PUT
271-
if(body != null && method.equalsIgnoreCase("post") || method.equalsIgnoreCase("put")) {
272-
Long tsLong = System.currentTimeMillis()/1000;
273-
String ts = tsLong.toString();
274-
String boundary = "RNFetchBlob".concat(ts);
275-
MultipartEntityBuilder form = MultipartEntityBuilder.create();
276-
form.setBoundary(boundary);
277-
for( int i = 0; i< body.size(); i++) {
278-
ReadableMap map = body.getMap(i);
279-
String name = map.getString("name");
280-
if(!map.hasKey("data"))
281-
continue;
282-
String data = map.getString("data");
283-
// file field
284-
if(map.hasKey("filename")) {
285-
String filename = map.getString("filename");
286-
// upload from storage
287-
if(data.startsWith(filePathPrefix)) {
288-
File file = new File(data.substring(filePathPrefix.length()));
289-
form.addBinaryBody(name, file, ContentType.APPLICATION_OCTET_STREAM, filename);
290-
}
291-
// base64 embedded file content
292-
else {
293-
form.addBinaryBody(name, Base64.decode(data, 0), ContentType.APPLICATION_OCTET_STREAM, filename);
294-
}
295-
}
296-
// data field
297-
else {
298-
form.addTextBody(name, map.getString("data"));
299-
}
300-
}
301-
entity = form.build();
302-
req.addHeader("Content-Type", headers.getString("Content-Type") + "; charset=utf8; boundary=" + boundary);
303-
}
304-
305-
AsyncHttpResponseHandler handler;
306-
307-
// create handler
308-
if(config.fileCache || config.path != null) {
309-
handler = new RNFetchBlobFileHandler(this.getReactApplicationContext(), taskId, config, callback);
310-
// if path format invalid, throw error
311-
if (!((RNFetchBlobFileHandler)handler).isValid) {
312-
callback.invoke("RNFetchBlob fetch error, configuration path `"+ config.path +"` is not a valid path.");
313-
return;
314-
}
315-
}
316-
else
317-
handler = new RNFetchBlobBinaryHandler(this.getReactApplicationContext(), taskId, callback);
318-
319-
// send request
320-
switch(method.toLowerCase()) {
321-
case "get" :
322-
req.get(url, handler);
323-
break;
324-
case "post" :
325-
req.post(this.getReactApplicationContext(), url, entity, "multipart/form-data; charset=utf8", handler);
326-
break;
327-
case "put" :
328-
req.put(this.getReactApplicationContext(), url, entity, "multipart/form-data",handler);
329-
break;
330-
case "delete" :
331-
req.delete(url, handler);
332-
break;
333-
}
334-
} catch(Exception error) {
335-
callback.invoke( "RNFetchBlob serialize request data failed: " + error.getMessage() + error.getCause());
336-
}
337-
168+
new RNFetchBlobReq(this.getReactApplicationContext(), options, taskId, method, url, headers, body, callback).run();
338169
}
339170

340171
}

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ public class RNFetchBlobConfig {
1313
public String path;
1414
public String appendExt;
1515
public ReadableMap addAndroidDownloads;
16+
public Boolean trusty;
1617

1718
RNFetchBlobConfig(ReadableMap options) {
1819
if(options == null)
1920
return;
2021
this.fileCache = options.hasKey("fileCache") ? options.getBoolean("fileCache") : false;
2122
this.path = options.hasKey("path") ? options.getString("path") : null;
2223
this.appendExt = options.hasKey("appendExt") ? options.getString("appendExt") : "";
24+
this.trusty = options.hasKey("trusty") ? options.getBoolean("trusty") : false;
2325
if(options.hasKey("addAndroidDownloads")) {
2426
this.addAndroidDownloads = options.getMap("addAndroidDownloads");
2527
}

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobPackage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.List;
2020

2121
/**
22-
* Created by xeiyan on 2016/4/29.
22+
* Created by wkh237 on 2016/4/29.
2323
*/
2424
public class RNFetchBlobPackage implements ReactPackage {
2525

0 commit comments

Comments
 (0)