ソースを参照

Add android timeout exception handling. #44

Fix Blob constructor error
Ben Hsieh 7 年 前
コミット
5430f4ff24
共有5 個のファイルを変更した31 個の追加9 個の削除を含む
  1. 26
    7
      src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java
  2. 1
    0
      src/index.js
  3. 1
    0
      src/polyfill/Blob.js
  4. 2
    2
      src/polyfill/XMLHttpRequest.js
  5. 1
    0
      src/polyfill/index.js

+ 26
- 7
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java ファイルの表示

82
     RequestType requestType;
82
     RequestType requestType;
83
     ResponseType responseType;
83
     ResponseType responseType;
84
     boolean timeout = false;
84
     boolean timeout = false;
85
+    WritableMap respInfo;
85
 
86
 
86
     public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
87
     public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
87
         this.method = method;
88
         this.method = method;
250
                     break;
251
                     break;
251
 
252
 
252
                 case WithoutBody:
253
                 case WithoutBody:
253
-                    builder.method(method, null);
254
+                    if(method.equalsIgnoreCase("POST") || method.equalsIgnoreCase("PUT"))
255
+                    {
256
+                        builder.method(method, RequestBody.create(null, new byte[0]));
257
+                    }
258
+                    else
259
+                        builder.method(method, null);
254
                     break;
260
                     break;
255
             }
261
             }
256
 
262
 
291
                     return chain.proceed(chain.request());
297
                     return chain.proceed(chain.request());
292
                 }
298
                 }
293
             });
299
             });
294
-            
295
-            if(options.timeout != -1) {
300
+
301
+            if(options.timeout > 0) {
296
                 clientBuilder.connectTimeout(options.timeout, TimeUnit.SECONDS);
302
                 clientBuilder.connectTimeout(options.timeout, TimeUnit.SECONDS);
303
+                clientBuilder.readTimeout(options.timeout, TimeUnit.SECONDS);
297
             }
304
             }
298
 
305
 
299
             OkHttpClient client = clientBuilder.build();
306
             OkHttpClient client = clientBuilder.build();
300
             Call call = client.newCall(req);
307
             Call call = client.newCall(req);
301
             taskTable.put(taskId, call);
308
             taskTable.put(taskId, call);
302
             call.enqueue(new okhttp3.Callback() {
309
             call.enqueue(new okhttp3.Callback() {
310
+
303
                 @Override
311
                 @Override
304
                 public void onFailure(Call call, IOException e) {
312
                 public void onFailure(Call call, IOException e) {
305
-                    callback.invoke(e.getLocalizedMessage(), null);
313
+                    if(respInfo == null) {
314
+                        respInfo = Arguments.createMap();
315
+                    }
316
+
317
+                    // check if this error caused by timeout
318
+                    if(e.getClass().equals(SocketTimeoutException.class)) {
319
+                        respInfo.putBoolean("timeout", true);
320
+                        callback.invoke("request timed out.", respInfo, null);
321
+                    }
322
+                    else
323
+                        callback.invoke(e.getLocalizedMessage(), respInfo, null);
306
                 }
324
                 }
307
 
325
 
308
                 @Override
326
                 @Override
309
                 public void onResponse(Call call, Response response) throws IOException {
327
                 public void onResponse(Call call, Response response) throws IOException {
310
                     ReadableMap notifyConfig = options.addAndroidDownloads;
328
                     ReadableMap notifyConfig = options.addAndroidDownloads;
329
+                    respInfo = getResponseInfo(response);
311
                     // Download manager settings
330
                     // Download manager settings
312
                     if(notifyConfig != null ) {
331
                     if(notifyConfig != null ) {
313
                         String title = "", desc = "", mime = "text/plain";
332
                         String title = "", desc = "", mime = "text/plain";
333
         } catch (Exception error) {
352
         } catch (Exception error) {
334
             error.printStackTrace();
353
             error.printStackTrace();
335
             taskTable.remove(taskId);
354
             taskTable.remove(taskId);
336
-            callback.invoke("RNFetchBlob request error: " + error.getMessage() + error.getCause());
355
+            callback.invoke("RNFetchBlob request error: " + error.getMessage() + error.getCause(), this.respInfo);
337
         }
356
         }
338
     }
357
     }
339
 
358
 
405
         }
424
         }
406
         info.putMap("headers", headers);
425
         info.putMap("headers", headers);
407
         Headers h = resp.headers();
426
         Headers h = resp.headers();
408
-        if(getHeaderIgnoreCases(h, "content-type").equalsIgnoreCase("text/plain")) {
427
+        if(getHeaderIgnoreCases(h, "content-type").equalsIgnoreCase("text/")) {
409
             info.putString("respType", "text");
428
             info.putString("respType", "text");
410
         }
429
         }
411
-        else if(getHeaderIgnoreCases(h, "content-type").equalsIgnoreCase("application/json")) {
430
+        else if(getHeaderIgnoreCases(h, "content-type").contains("application/json")) {
412
             info.putString("respType", "json");
431
             info.putString("respType", "json");
413
         }
432
         }
414
         else if(getHeaderIgnoreCases(h, "content-type").length() < 1) {
433
         else if(getHeaderIgnoreCases(h, "content-type").length() < 1) {

+ 1
- 0
src/index.js ファイルの表示

150
       subscription.remove()
150
       subscription.remove()
151
       subscriptionUpload.remove()
151
       subscriptionUpload.remove()
152
       stateEvent.remove()
152
       stateEvent.remove()
153
+      info = info ? info : {}
153
       if(err)
154
       if(err)
154
         reject(new Error(err, info))
155
         reject(new Error(err, info))
155
       else {
156
       else {

+ 1
- 0
src/polyfill/Blob.js ファイルの表示

47
    *                    by default
47
    *                    by default
48
    */
48
    */
49
   constructor(data:any, cType:any) {
49
   constructor(data:any, cType:any) {
50
+    super()
50
     cType = cType || {}
51
     cType = cType || {}
51
     this.cacheName = getBlobName()
52
     this.cacheName = getBlobName()
52
     this.isRNFetchBlobPolyfill = true
53
     this.isRNFetchBlobPolyfill = true

+ 2
- 2
src/polyfill/XMLHttpRequest.js ファイルの表示

124
       body = JSON.stringify(body)
124
       body = JSON.stringify(body)
125
     }
125
     }
126
     else
126
     else
127
-      body = body.toString()
127
+      body = body ? body.toString() : body
128
 
128
 
129
     this._task = RNFetchBlob
129
     this._task = RNFetchBlob
130
                   .config({ auto: true, timeout : this._timeout })
130
                   .config({ auto: true, timeout : this._timeout })
299
     this._onreadystatechange = fn
299
     this._onreadystatechange = fn
300
   }
300
   }
301
 
301
 
302
-  get onreadystatechange(fn:() => void) {
302
+  get onreadystatechange() {
303
     return this._onreadystatechange
303
     return this._onreadystatechange
304
   }
304
   }
305
 
305
 

+ 1
- 0
src/polyfill/index.js ファイルの表示

3
 import XMLHttpRequest from './XMLHttpRequest.js'
3
 import XMLHttpRequest from './XMLHttpRequest.js'
4
 import FormData from './FormData.js'
4
 import FormData from './FormData.js'
5
 import ProgressEvent from './ProgressEvent'
5
 import ProgressEvent from './ProgressEvent'
6
+import Event from './Event'
6
 
7
 
7
 export default {
8
 export default {
8
   Blob, File, XMLHttpRequest, FormData, ProgressEvent, Event
9
   Blob, File, XMLHttpRequest, FormData, ProgressEvent, Event