Browse Source

Android code refactor

Ben Hsieh 8 years ago
parent
commit
b578764d3c
1 changed files with 8 additions and 10 deletions
  1. 8
    10
      src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

+ 8
- 10
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

156
 
156
 
157
         // find cached result if `key` property exists
157
         // find cached result if `key` property exists
158
         String cacheKey = this.taskId;
158
         String cacheKey = this.taskId;
159
-		String ext = this.options.appendExt != "" ? "." + this.options.appendExt : "";
159
+		String ext = this.options.appendExt.isEmpty() ? "." + this.options.appendExt : "";
160
 
160
 
161
        	if (this.options.key != null) {
161
        	if (this.options.key != null) {
162
            cacheKey = RNFetchBlobUtils.getMD5(this.options.key);
162
            cacheKey = RNFetchBlobUtils.getMD5(this.options.key);
174
 
174
 
175
         if(this.options.path != null)
175
         if(this.options.path != null)
176
             this.destPath = this.options.path;
176
             this.destPath = this.options.path;
177
-        else if(this.options.fileCache == true)
177
+        else if(this.options.fileCache)
178
             this.destPath = RNFetchBlobFS.getTmpPath(RNFetchBlob.RCTContext, cacheKey) + ext;
178
             this.destPath = RNFetchBlobFS.getTmpPath(RNFetchBlob.RCTContext, cacheKey) + ext;
179
 
179
 
180
         OkHttpClient.Builder clientBuilder;
180
         OkHttpClient.Builder clientBuilder;
212
                 if(rawRequestBodyArray != null) {
212
                 if(rawRequestBodyArray != null) {
213
                     requestType = RequestType.Form;
213
                     requestType = RequestType.Form;
214
                 }
214
                 }
215
-                else if(cType == null || cType.isEmpty()) {
215
+                else if(cType.isEmpty()) {
216
                     builder.header("Content-Type", "application/octet-stream");
216
                     builder.header("Content-Type", "application/octet-stream");
217
                     requestType = RequestType.SingleFile;
217
                     requestType = RequestType.SingleFile;
218
                 }
218
                 }
280
 
280
 
281
             final Request req = builder.build();
281
             final Request req = builder.build();
282
 
282
 
283
-            // Create response body depends on the responseType
283
+            // Add request interceptor for upload progress event
284
             clientBuilder.addInterceptor(new Interceptor() {
284
             clientBuilder.addInterceptor(new Interceptor() {
285
                 @Override
285
                 @Override
286
                 public Response intercept(Chain chain) throws IOException {
286
                 public Response intercept(Chain chain) throws IOException {
310
                         }
310
                         }
311
                         return originalResponse.newBuilder().body(extended).build();
311
                         return originalResponse.newBuilder().body(extended).build();
312
                     }
312
                     }
313
-                    catch (SocketTimeoutException e){
313
+                    catch(SocketException e) {
314
                         timeout = true;
314
                         timeout = true;
315
                     }
315
                     }
316
-                    catch (SocketException ex) {
316
+                    catch (SocketTimeoutException e ){
317
                         timeout = true;
317
                         timeout = true;
318
-                    }
319
-                    catch(Exception ex) {
318
+                    } catch(Exception ex) {
320
                         RNFetchBlobUtils.emitWarningEvent("RNFetchBlob error when sending request : " + ex.getLocalizedMessage());
319
                         RNFetchBlobUtils.emitWarningEvent("RNFetchBlob error when sending request : " + ex.getLocalizedMessage());
321
-
322
                     }
320
                     }
323
                     return chain.proceed(chain.request());
321
                     return chain.proceed(chain.request());
324
                 }
322
                 }
611
                     }
609
                     }
612
                 }
610
                 }
613
                 else
611
                 else
614
-                    this.callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, filePath);
612
+                    this.callback.invoke("Download manager could not resolve downloaded file path.", RNFetchBlobConst.RNFB_RESPONSE_PATH, null);
615
             }
613
             }
616
         }
614
         }
617
     }
615
     }