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>() { @Override public 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>() { @Override public void accept(Boolean b) throws Exception { if (b) { count.addAndGet(1); } } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { } }, new Action() { @Override public 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>() { @Override public void accept(Disposable disposable) throws Exception { } }); }