|
@@ -12,11 +12,13 @@ import android.util.Log;
|
12
|
12
|
|
13
|
13
|
import com.RNFetchBlob.Response.RNFetchBlobDefaultResp;
|
14
|
14
|
import com.RNFetchBlob.Response.RNFetchBlobFileResp;
|
|
15
|
+import com.facebook.react.bridge.Arguments;
|
15
|
16
|
import com.facebook.react.bridge.Callback;
|
16
|
17
|
import com.facebook.react.bridge.ReactApplicationContext;
|
17
|
18
|
import com.facebook.react.bridge.ReadableArray;
|
18
|
19
|
import com.facebook.react.bridge.ReadableMap;
|
19
|
20
|
import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
|
21
|
+import com.facebook.react.bridge.WritableMap;
|
20
|
22
|
|
21
|
23
|
import java.io.ByteArrayInputStream;
|
22
|
24
|
import java.io.File;
|
|
@@ -28,6 +30,7 @@ import java.net.URL;
|
28
|
30
|
import java.util.HashMap;
|
29
|
31
|
|
30
|
32
|
import okhttp3.Call;
|
|
33
|
+import okhttp3.Headers;
|
31
|
34
|
import okhttp3.Interceptor;
|
32
|
35
|
import okhttp3.MediaType;
|
33
|
36
|
import okhttp3.OkHttpClient;
|
|
@@ -36,6 +39,7 @@ import okhttp3.RequestBody;
|
36
|
39
|
import okhttp3.Response;
|
37
|
40
|
import okhttp3.ResponseBody;
|
38
|
41
|
import okhttp3.FormBody;
|
|
42
|
+import okhttp3.internal.framed.Header;
|
39
|
43
|
|
40
|
44
|
/**
|
41
|
45
|
* Created by wkh237 on 2016/6/21.
|
|
@@ -308,7 +312,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
308
|
312
|
case KeepInMemory:
|
309
|
313
|
try {
|
310
|
314
|
byte [] b = resp.body().bytes();
|
311
|
|
- callback.invoke(null, android.util.Base64.encodeToString(b,Base64.NO_WRAP));
|
|
315
|
+ callback.invoke(null, getResponseInfo(resp), android.util.Base64.encodeToString(b,Base64.NO_WRAP));
|
312
|
316
|
} catch (IOException e) {
|
313
|
317
|
callback.invoke("RNFetchBlob failed to encode response data to BASE64 string.", null);
|
314
|
318
|
}
|
|
@@ -319,11 +323,11 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
319
|
323
|
} catch (Exception ignored) {
|
320
|
324
|
|
321
|
325
|
}
|
322
|
|
- callback.invoke(null, this.destPath);
|
|
326
|
+ callback.invoke(null, getResponseInfo(resp), this.destPath);
|
323
|
327
|
break;
|
324
|
328
|
default:
|
325
|
329
|
try {
|
326
|
|
- callback.invoke(null, new String(resp.body().bytes(), "UTF-8"));
|
|
330
|
+ callback.invoke(null, getResponseInfo(resp), new String(resp.body().bytes(), "UTF-8"));
|
327
|
331
|
} catch (IOException e) {
|
328
|
332
|
callback.invoke("RNFetchBlob failed to encode response data to UTF8 string.", null);
|
329
|
333
|
}
|
|
@@ -333,6 +337,42 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
333
|
337
|
taskTable.remove(taskId);
|
334
|
338
|
}
|
335
|
339
|
|
|
340
|
+ private WritableMap getResponseInfo(Response resp) {
|
|
341
|
+ WritableMap info = Arguments.createMap();
|
|
342
|
+ info.putInt("status", resp.code());
|
|
343
|
+ info.putString("state", "2");
|
|
344
|
+ info.putString("taskId", this.taskId);
|
|
345
|
+ WritableMap headers = Arguments.createMap();
|
|
346
|
+ for(int i =0;i< resp.headers().size();i++) {
|
|
347
|
+ headers.putString(resp.headers().name(i), resp.headers().value(i));
|
|
348
|
+ }
|
|
349
|
+ info.putMap("headers", headers);
|
|
350
|
+ Headers h = resp.headers();
|
|
351
|
+ if(getHeaderIgnoreCases(h, "content-type").equalsIgnoreCase("text/plain"))
|
|
352
|
+ {
|
|
353
|
+ info.putString("respType", "text");
|
|
354
|
+ }
|
|
355
|
+ else if(getHeaderIgnoreCases(h, "content-type").equalsIgnoreCase("application/json"))
|
|
356
|
+ {
|
|
357
|
+ info.putString("respType", "json");
|
|
358
|
+ }
|
|
359
|
+ else if(getHeaderIgnoreCases(h, "content-type").length() < 1)
|
|
360
|
+ {
|
|
361
|
+ info.putString("respType", "blob");
|
|
362
|
+ }
|
|
363
|
+ else
|
|
364
|
+ {
|
|
365
|
+ info.putString("respType", "text");
|
|
366
|
+ }
|
|
367
|
+ return info;
|
|
368
|
+ }
|
|
369
|
+
|
|
370
|
+ private String getHeaderIgnoreCases(Headers headers, String field) {
|
|
371
|
+ String val = headers.get(field);
|
|
372
|
+ if(val != null) return val;
|
|
373
|
+ return headers.get(field.toLowerCase()) == null ? "" : headers.get(field.toLowerCase());
|
|
374
|
+ }
|
|
375
|
+
|
336
|
376
|
/**
|
337
|
377
|
* Build request body by given string
|
338
|
378
|
* @param body Content of request body in UTF8 string format.
|