日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Retrofit 一次个请求下载多个图片

發布時間:2023/12/31 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Retrofit 一次个请求下载多个图片 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.封裝一個工廠,單例,

??????? 相關得依賴

implementation 'com.squareup.retrofit2:retrofit:2.4.0' //必須 implementation 'com.squareup.retrofit2:converter-gson:2.4.0' //json解析 implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0' implementation 'com.trello.rxlifecycle2:rxlifecycle-components:2.2.1' implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0' //必須 public class DownPictureFactory { // private static final String BASE_API = "http://api2.diction.com/Api/";private static final String BASE_API = "http://epdapi2.diction.diexun.com/Api/";private static final int CONNECT_TIME_OUT = 60;//連接超時時長x秒private static final int READ_TIME_OUT = 60;//讀數據超時時長x秒private static final int WRITE_TIME_OUT = 60;//寫數據接超時時長x秒private static DownPictureFactory mInstance = null;private Retrofit mApiService;public static DownPictureFactory getInstance() {if (mInstance == null) {synchronized (DownPictureFactory.class) {if (mInstance == null) {mInstance = new DownPictureFactory();}}}return mInstance;}private DownPictureFactory() { // GsonConverterFactory mConverterFactory = GsonConverterFactory.create(new GsonBuilder().create()); // RxJava2CallAdapterFactory mAdapterFactory = RxJava2CallAdapterFactory.create();mApiService = new Retrofit.Builder().baseUrl(BASE_API).client(configOkHttpClient()).addConverterFactory(GsonConverterFactory.create()) // json解析.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) // .addCallAdapterFactory(mAdapterFactory) // 支持rxjava.build();}private OkHttpClient configOkHttpClient() {OkHttpClient.Builder mBuilder = new OkHttpClient().newBuilder();mBuilder.connectTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS);mBuilder.writeTimeout(WRITE_TIME_OUT, TimeUnit.SECONDS);mBuilder.readTimeout(READ_TIME_OUT, TimeUnit.SECONDS);mBuilder.addInterceptor(new BaseUrlInterceptor());if (AppManager.getInstance().isShowHttpLog) {HttpLoggingInterceptor logging = new HttpLoggingInterceptor();logging.setLevel(HttpLoggingInterceptor.Level.BODY);mBuilder.addInterceptor(logging);}return mBuilder.build();}public Retrofit getRetrofit(){return mApiService;}}

2.ApiService接口

@Streaming @GET Observable<ResponseBody> downloadImage(@Url String image);

3.保存方法

?ArrayList<String> images? 就是下載鏈接的集合

public void downLoadIamge(ArrayList<String> images){PrintUtilsJava.pringtLog("downLoadIamge-----sava 001");Retrofit retrofit = DownPictureFactory.getInstance().getRetrofit();ApiService apiService = retrofit.create(ApiService.class);AtomicInteger count = new AtomicInteger();ArrayList<Observable<Boolean>> observables = new ArrayList<>();for (String image : images){observables.add(apiService.downloadImage(image).subscribeOn(Schedulers.io()).map(new Function<ResponseBody, Boolean>() {@Overridepublic Boolean apply(@NonNull ResponseBody responseBody) throws Exception {PrintUtilsJava.pringtLog("downLoadIamge-----apply 001");saveIo(responseBody.byteStream(),image);//在這里保存圖片return true;}}));}Disposable subscribe = Observable.merge(observables).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Boolean>() {@Overridepublic void accept(Boolean b) throws Exception {if (b) {count.addAndGet(1);}}}, new Consumer<Throwable>() {@Overridepublic void accept(Throwable throwable) throws Exception {}}, new Action() {@Overridepublic void run() throws Exception {PrintUtilsJava.pringtLog("downLoadIamge-----apply 003");// 下載成功的數量 和 圖片集合的數量一致,說明全部下載成功了if (images.size() == count.get()) {ToastUtils.showShort("保存成功");} else {if (count.get() == 0) {ToastUtils.showShort("保存失敗");} else {ToastUtils.showShort("因網絡問題 保存成功" + count + ",保存失敗" + (images.size() - count.get()));}}}}, new Consumer<Disposable>() {@Overridepublic void accept(Disposable disposable) throws Exception {}});}

總結

以上是生活随笔為你收集整理的Retrofit 一次个请求下载多个图片的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。