Browse Source

Change format builder function

Prevent app crash when form data contains undefined data field #90 .
Ben Hsieh 8 years ago
parent
commit
35973e24e4
1 changed files with 7 additions and 6 deletions
  1. 7
    6
      src/android/src/main/java/com/RNFetchBlob/RNFetchBlobBody.java

+ 7
- 6
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobBody.java View File

242
         ArrayList<FormField> list = new ArrayList<>();
242
         ArrayList<FormField> list = new ArrayList<>();
243
         ReactApplicationContext ctx = RNFetchBlob.RCTContext;
243
         ReactApplicationContext ctx = RNFetchBlob.RCTContext;
244
         for(int i = 0;i < form.size(); i++) {
244
         for(int i = 0;i < form.size(); i++) {
245
-            ReadableMap field = form.getMap(i);
246
-            list.add(new FormField(field));
247
-            String data = field.getString("data");
248
-            if (field.hasKey("filename")) {
245
+            FormField field = new FormField(form.getMap(i));
246
+            list.add(field);
247
+            String data = field.data;
248
+            if (field.filename != null) {
249
                 // upload from storage
249
                 // upload from storage
250
                 if (data.startsWith(RNFetchBlobConst.FILE_PREFIX)) {
250
                 if (data.startsWith(RNFetchBlobConst.FILE_PREFIX)) {
251
                     String orgPath = data.substring(RNFetchBlobConst.FILE_PREFIX.length());
251
                     String orgPath = data.substring(RNFetchBlobConst.FILE_PREFIX.length());
274
             }
274
             }
275
             // data field
275
             // data field
276
             else {
276
             else {
277
-                total += field.getString("data").length();
277
+                total += field.data != null ? field.data.length() : 0;
278
             }
278
             }
279
         }
279
         }
280
         contentLength = total;
280
         contentLength = total;
301
             else {
301
             else {
302
                 mime = filename == null ? "text/plain" : "application/octet-stream";
302
                 mime = filename == null ? "text/plain" : "application/octet-stream";
303
             }
303
             }
304
-            if(rawData.hasKey("data"))
304
+            if(rawData.hasKey("data")) {
305
                 data = rawData.getString("data");
305
                 data = rawData.getString("data");
306
+            }
306
         }
307
         }
307
     }
308
     }
308
 
309