简体   繁体   中英

How to load ResponseBody's for multiple images in Retrofit2 (with and without Rx)

I develop an application that accepts JSON (consisting of links to pictures), then through these links, I get the header of the response and pull out contentlength() and contenttype() and source() from there in order to write these pictures to external storage.

JSON example:

[
  {
    "imageUrl": "https://bipbap.ru/wp-content/uploads/2018/01/57a741c2d70811566558e141.png"
  },
  {
    "imageUrl": "https://bipbap.ru/wp-content/uploads/2018/01/57a741c2d70811566558e141.png"
  },
  {
    "imageUrl": "https://bipbap.ru/wp-content/uploads/2018/01/57a741c2d70811566558e141.png"
  },
  {
    "imageUrl": "https://bipbap.ru/wp-content/uploads/2018/01/57a741c2d70811566558e141.png"
  }
]

Code example:

public interface RetrofitApi {

    @GET("bins/xdfi8")
    Call<List<Image>> getImage();

    @GET
    Call<ResponseBody> getImage(@Url String url);
}
</code>

// MainActivity.java

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.myjson.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

     final RetrofitApi retrofitApi = retrofit.create(RetrofitApi.class);

        Call<List<Image>> call = retrofitApi.getImages();


        call.enqueue(new Callback<List<Image>>() {
            @Override
            public void onResponse(Call<List<Image>> call, Response<List<Image>> response) {

                if (!response.isSuccessful()) {
                    textViewResult.setText("Code: " + response.code());
                    return;
                }

                List<Image> images = response.body();

                for (Image image : images) {
                    Call<ResponseBody> responseBodyCall = retrofitApi.getResponseBody(image.getImageUrl());
                    responseBodyCall.enqueue(new Callback<ResponseBody>() {
                        @Override
                        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                            String content = "";
                            content += response.body().contentType().subtype() + "\n";
                            content += response.body().contentLength() + "\n\n";
                            // for example print contenttype and contentlength in TextView
                            textViewResult.append(content);
                        }

                        @Override
                        public void onFailure(Call<ResponseBody> call, Throwable t) {

                        }
                    });

            @Override
            public void onFailure(Call<List<Image>> call, Throwable t) {

            }
});
}

}

Image.java

   public class Image {

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    private String imageUrl;
}

At the moment, one call is being put into another call, how to avoid it?

Also interested in the method if we used RxJava and RetrofitApi would take this form:

public interface RetrofitApi {

    @GET("bins/xdfi8")
    Observable<List<Image>> getImage();

    @GET
    Observable<Response<ResponseBody>> getImage(@Url String url);
}

Lets say you have this api:

public interface RetrofitApi {

    @GET("bins/xdfi8")
    Observable<List<Image>> getImage();

    @GET
    Observable<Response<ResponseBody>> getImage(@Url String url);
}

Then in activity:

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.myjson.com/")
    .addConverterFactory(GsonConverterFactory.create())
    .build();

final RetrofitApi retrofitApi = retrofit.create(RetrofitApi.class);

retrofitApi.getImages() // Observable<List<Image>>
    .flatMapIterable(images -> images) // Observable<Image>
    .flatMap(image -> retrofitApi.getImage(image.getImageUrl()))
            .onErrorResumeNext(Observable.empty())) // Observable<ResponseBody>
    .subscribeOn(Schedulers.io()) // run using io scheduler
    .observeOn(AndroidSchedulers.mainThread()) // observe in main thread
    .subscribe(result -> {
        ...
        textViewResult.append(content);
    }, error -> {
        textViewResult.setText("Code: " + error);
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM