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

Commit 7edc5c8

Browse files
committed
Add #29 Android implementation
1 parent e48e6ca commit 7edc5c8

File tree

4 files changed

+212
-150
lines changed

4 files changed

+212
-150
lines changed

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

Lines changed: 4 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
import com.loopj.android.http.AsyncHttpClient;
1818
import com.loopj.android.http.AsyncHttpResponseHandler;
1919
import com.loopj.android.http.Base64;
20+
import com.loopj.android.http.MySSLSocketFactory;
2021
import com.loopj.android.http.RequestParams;
2122

2223
import java.io.ByteArrayOutputStream;
2324
import java.io.File;
25+
import java.security.KeyStore;
2426
import java.util.HashMap;
2527
import java.util.Map;
2628

@@ -156,159 +158,12 @@ public void readStream(String path, String encoding, int bufferSize) {
156158

157159
@ReactMethod
158160
public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
159-
160-
RNFetchBlobConfig config = new RNFetchBlobConfig(options);
161-
162-
try {
163-
AsyncHttpClient req = new AsyncHttpClient();
164-
165-
AbstractHttpEntity entity = null;
166-
167-
// set headers
168-
ReadableMapKeySetIterator it = headers.keySetIterator();
169-
while (it.hasNextKey()) {
170-
String key = it.nextKey();
171-
req.addHeader(key, headers.getString(key));
172-
}
173-
174-
// set body for POST and PUT
175-
if(body != null && method.equalsIgnoreCase("post") || method.equalsIgnoreCase("put")) {
176-
177-
byte [] blob;
178-
// upload from storage
179-
if(body.startsWith(filePathPrefix)) {
180-
String filePath = body.substring(filePathPrefix.length());
181-
entity = new FileEntity(new File(filePath));
182-
}
183-
else {
184-
blob = Base64.decode(body, 0);
185-
entity = new ByteArrayEntity(blob);
186-
}
187-
entity.setContentType(headers.getString("Content-Type"));
188-
}
189-
190-
AsyncHttpResponseHandler handler;
191-
192-
// create handler
193-
if(config.fileCache || config.path != null) {
194-
handler = new RNFetchBlobFileHandler(this.getReactApplicationContext(), taskId, config, callback);
195-
// if path format invalid, throw error
196-
if (!((RNFetchBlobFileHandler)handler).isValid) {
197-
callback.invoke("RNFetchBlob fetch error, configuration path `"+ config.path +"` is not a valid path.");
198-
return;
199-
}
200-
}
201-
else
202-
handler = new RNFetchBlobBinaryHandler(this.getReactApplicationContext(), taskId, callback);
203-
204-
// send request
205-
switch(method.toLowerCase()) {
206-
case "get" :
207-
req.get(url, handler);
208-
break;
209-
case "post" :
210-
req.post(this.getReactApplicationContext(), url, entity, "octet-stream", handler);
211-
break;
212-
case "put" :
213-
req.put(this.getReactApplicationContext(), url, entity, "octet-stream",handler);
214-
break;
215-
case "delete" :
216-
req.delete(url, handler);
217-
break;
218-
}
219-
} catch(Exception error) {
220-
callback.invoke( "RNFetchBlob serialize request data failed: " + error.getMessage() + error.getCause());
221-
}
222-
161+
new RNFetchBlobReq(this.getReactApplicationContext(), options, taskId, method, url, headers, body, callback).run();
223162
}
224163

225164
@ReactMethod
226165
public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) {
227-
228-
RNFetchBlobConfig config = new RNFetchBlobConfig(options);
229-
try {
230-
231-
AsyncHttpClient req = new AsyncHttpClient();
232-
233-
HttpEntity entity = null;
234-
235-
// set headers
236-
if(headers != null) {
237-
ReadableMapKeySetIterator it = headers.keySetIterator();
238-
while (it.hasNextKey()) {
239-
String key = it.nextKey();
240-
req.addHeader(key, headers.getString(key));
241-
}
242-
}
243-
244-
// set body for POST and PUT
245-
if(body != null && method.equalsIgnoreCase("post") || method.equalsIgnoreCase("put")) {
246-
Long tsLong = System.currentTimeMillis()/1000;
247-
String ts = tsLong.toString();
248-
String boundary = "RNFetchBlob".concat(ts);
249-
MultipartEntityBuilder form = MultipartEntityBuilder.create();
250-
form.setBoundary(boundary);
251-
for( int i = 0; i< body.size(); i++) {
252-
ReadableMap map = body.getMap(i);
253-
String name = map.getString("name");
254-
if(!map.hasKey("data"))
255-
continue;
256-
String data = map.getString("data");
257-
// file field
258-
if(map.hasKey("filename")) {
259-
String filename = map.getString("filename");
260-
// upload from storage
261-
if(data.startsWith(filePathPrefix)) {
262-
File file = new File(data.substring(filePathPrefix.length()));
263-
form.addBinaryBody(name, file, ContentType.APPLICATION_OCTET_STREAM, filename);
264-
}
265-
// base64 embedded file content
266-
else {
267-
form.addBinaryBody(name, Base64.decode(data, 0), ContentType.APPLICATION_OCTET_STREAM, filename);
268-
}
269-
}
270-
// data field
271-
else {
272-
form.addTextBody(name, map.getString("data"));
273-
}
274-
}
275-
entity = form.build();
276-
req.addHeader("Content-Type", headers.getString("Content-Type") + "; charset=utf8; boundary=" + boundary);
277-
}
278-
279-
AsyncHttpResponseHandler handler;
280-
281-
// create handler
282-
if(config.fileCache || config.path != null) {
283-
handler = new RNFetchBlobFileHandler(this.getReactApplicationContext(), taskId, config, callback);
284-
// if path format invalid, throw error
285-
if (!((RNFetchBlobFileHandler)handler).isValid) {
286-
callback.invoke("RNFetchBlob fetch error, configuration path `"+ config.path +"` is not a valid path.");
287-
return;
288-
}
289-
}
290-
else
291-
handler = new RNFetchBlobBinaryHandler(this.getReactApplicationContext(), taskId, callback);
292-
293-
// send request
294-
switch(method.toLowerCase()) {
295-
case "get" :
296-
req.get(url, handler);
297-
break;
298-
case "post" :
299-
req.post(this.getReactApplicationContext(), url, entity, "multipart/form-data; charset=utf8", handler);
300-
break;
301-
case "put" :
302-
req.put(this.getReactApplicationContext(), url, entity, "multipart/form-data",handler);
303-
break;
304-
case "delete" :
305-
req.delete(url, handler);
306-
break;
307-
}
308-
} catch(Exception error) {
309-
callback.invoke( "RNFetchBlob serialize request data failed: " + error.getMessage() + error.getCause());
310-
}
311-
166+
new RNFetchBlobReq(this.getReactApplicationContext(), options, taskId, method, url, headers, body, callback).run();
312167
}
313168

314169
}

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)