简体   繁体   English

如何在Retrofit2中加载多个图像的ResponseBody(带有和不带有Rx)

[英]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 (由图片的链接组成)的应用程序,然后通过这些链接,获得响应的标头并从那里拉出contentlength()contenttype()source()以便将这些图片写到外置储存。

JSON example: JSON示例:

[
  {
    "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 // 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 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: 如果我们使用RxJavaRetrofitApi也对该方法感兴趣,它将采用以下形式:

public interface RetrofitApi {

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

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

Lets say you have this api: 可以说您有这个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);
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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