title: How to repeat same API request multiple times with different parameters and do a second network call to send those data back to server using RxJava
body: <p>I have to do a complex API call in a project I'm currently working on. Hear's the details of it.</p>
  
  <p>I have a two network calls one for image upload and second one to add product.</p>
  
  <pre><code>@FormUrlEncoded
  @POST("feed.asp")
  Observable&lt;RequestEnquiryAddResponse&gt; addRequestEnquriyProductRx(@Field("Access_Token") String accessToken, @Field("Product") JSONObject product);
  
  @Multipart
  @POST("feed-server.asp")
  Observable&lt;ProductImageUploadResponse&gt; uploadProductImageRx(@Part("Access_Token") RequestBody accessToken, @Part("Req") RequestBody req, @Part MultipartBody.Part filePart);
  </code></pre>
  
  <p>A product can have maximum 10 of product image links. These images are selected from gallery or captured by camera and store in app private local storage folder after cropping and adding filters.After successfully upload a image to the server image should be deleted form local storage. Image upload API call going to response with uploaded image link in server. Those all image links needed to make second network call which is app product.</p>
  
  <p>In addition to this requirements every Image upload request should retry 3 times in case of network call failure before try uploading next image.</p>
  
  <p>Since Rx java is good with chaining network call I wonder is this scenario is achievable using Rx java. I followed this <a href="https://stackoverflow.com/questions/41855472/how-to-repeat-same-network-request-multiple-timesdifferent-parameters-in-rxjav">link</a> and try to achieve it but I can't find a way to do second network call with all uploaded image links. This is what i got.</p>
  
  <pre><code> Observable&lt;Object&gt; retry = Observable
                      .range(0, ImageFilePaths.size())
                      .flatMap(new Function&lt;Integer, ObservableSource&lt;?&gt;&gt;() {
                          @Override
                          public ObservableSource&lt;ProductImageUploadResponse&gt; apply(Integer index){
  
                              JSONObject imageUploadKJsonObj = new JSONObject();
  
                              try {
                                  imageUploadKJsonObj.put("version", "1.0");
                                  imageUploadKJsonObj.put("action", "uploadimage");
                              } catch (JSONException e) {
                                  e.printStackTrace();
                              }
  
                              File file = new File(ImageFilePaths.get(index));
                              MultipartBody.Part filePart = MultipartBody.Part.createFormData("Image", file.getName(),
                                      RequestBody.create(MediaType.parse("image/*"), file));
                              RequestBody accessTokenRequestBody = RequestBody.create(MediaType.parse("text/plain"), accessToken);
                              RequestBody imageUploadRequestBody = RequestBody.create(MediaType.parse("text/plain"), imageUploadKJsonObj.toString());
  
                              return APIservice.uploadProductImageRx(accessTokenRequestBody, imageUploadRequestBody, filePart);
                          }
                      })
                      .retry(3);
  </code></pre>
  
  <p>I'am developing on android and using retrofit and Gson for API integration. image link can be found in image upload API call response JSON object (<code>ProductImageUploadResponse</code>) with the key value of <code>imagefullpath</code>.</p>
  
  <p>Thanks.</p>