Retrofit 一次个请求下载多个图片
生活随笔
收集整理的這篇文章主要介紹了
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 一次个请求下载多个图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (收集)一些好用的漏洞库网站:含工控安全
- 下一篇: 数字电子技术之触发器