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,7 +156,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
156 156
 
157 157
         // find cached result if `key` property exists
158 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 161
        	if (this.options.key != null) {
162 162
            cacheKey = RNFetchBlobUtils.getMD5(this.options.key);
@@ -174,7 +174,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
174 174
 
175 175
         if(this.options.path != null)
176 176
             this.destPath = this.options.path;
177
-        else if(this.options.fileCache == true)
177
+        else if(this.options.fileCache)
178 178
             this.destPath = RNFetchBlobFS.getTmpPath(RNFetchBlob.RCTContext, cacheKey) + ext;
179 179
 
180 180
         OkHttpClient.Builder clientBuilder;
@@ -212,7 +212,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
212 212
                 if(rawRequestBodyArray != null) {
213 213
                     requestType = RequestType.Form;
214 214
                 }
215
-                else if(cType == null || cType.isEmpty()) {
215
+                else if(cType.isEmpty()) {
216 216
                     builder.header("Content-Type", "application/octet-stream");
217 217
                     requestType = RequestType.SingleFile;
218 218
                 }
@@ -280,7 +280,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
280 280
 
281 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 284
             clientBuilder.addInterceptor(new Interceptor() {
285 285
                 @Override
286 286
                 public Response intercept(Chain chain) throws IOException {
@@ -310,15 +310,13 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
310 310
                         }
311 311
                         return originalResponse.newBuilder().body(extended).build();
312 312
                     }
313
-                    catch (SocketTimeoutException e){
313
+                    catch(SocketException e) {
314 314
                         timeout = true;
315 315
                     }
316
-                    catch (SocketException ex) {
316
+                    catch (SocketTimeoutException e ){
317 317
                         timeout = true;
318
-                    }
319
-                    catch(Exception ex) {
318
+                    } catch(Exception ex) {
320 319
                         RNFetchBlobUtils.emitWarningEvent("RNFetchBlob error when sending request : " + ex.getLocalizedMessage());
321
-
322 320
                     }
323 321
                     return chain.proceed(chain.request());
324 322
                 }
@@ -611,7 +609,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
611 609
                     }
612 610
                 }
613 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
     }