| | |
| | | package com.dayu.pipirrapp.net; |
| | | |
| | | import static com.dayu.pipirrapp.bean.net.UplodFileState.STATE_DONE; |
| | | import static com.dayu.pipirrapp.bean.net.UplodFileState.STATE_ERROR; |
| | | import static com.dayu.pipirrapp.bean.net.UplodFileState.STATE_UPDING; |
| | | import static com.luck.picture.lib.thread.PictureThreadUtils.runOnUiThread; |
| | | |
| | | import android.annotation.SuppressLint; |
| | |
| | | import com.dayu.pipirrapp.net.subscribers.CodeListener; |
| | | import com.dayu.pipirrapp.net.subscribers.ProgressSubscriber; |
| | | import com.dayu.pipirrapp.net.subscribers.SubscriberListener; |
| | | import com.dayu.pipirrapp.net.upload.IMGProgressLisener; |
| | | import com.dayu.pipirrapp.net.upload.ProgressListener; |
| | | import com.dayu.pipirrapp.net.upload.ProgressRequestBody; |
| | | import com.dayu.pipirrapp.net.upload.UploadFileListener; |
| | | import com.dayu.pipirrapp.utils.CleanDataUtils; |
| | |
| | | import com.dayu.pipirrapp.utils.MyJsonParser; |
| | | import com.dayu.pipirrapp.utils.MyLog; |
| | | import com.dayu.pipirrapp.utils.NetUtils; |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.internal.LinkedTreeMap; |
| | | |
| | | import java.io.File; |
| | |
| | | //文件上传失败重复次数 |
| | | int uplodFilerepeatSize = 3; |
| | | |
| | | |
| | | ApiService apiService; |
| | | // 管理订阅事件 |
| | | private CompositeDisposable compositeDisposable = new CompositeDisposable(); |
| | | private final CompositeDisposable compositeDisposable = new CompositeDisposable(); |
| | | |
| | | /** |
| | | * 初始化通信框架 |
| | |
| | | } |
| | | BaseResponse<T> response = new BaseResponse<>(); |
| | | response.setCode(tem.getCode()); |
| | | if (!TextUtils.isEmpty(tem.getMsg())){ |
| | | if (!TextUtils.isEmpty(tem.getMsg())) { |
| | | response.setMsg(tem.getMsg()); |
| | | }else { |
| | | } else { |
| | | response.setMsg(""); |
| | | } |
| | | |
| | |
| | | // } |
| | | |
| | | /** |
| | | * 上传文件 |
| | | * 上传图片文件 |
| | | * |
| | | * @param context |
| | | * @param uplodData |
| | | * @param listener |
| | | */ |
| | | public void uploadFile(final Context context, final UplodFileState uplodData, final UploadFileListener listener, final AddPictureAdapter adapter) { |
| | | MyLog.d("progressRequestBody>>>" + "path:" + uplodData.getFilePath() ); |
| | | File file = new File(uplodData.getFilePath()); |
| | | RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file); |
| | | ProgressRequestBody progressRequestBody = new ProgressRequestBody(requestBody, (bytesWritten, contentLength, done) -> { |
| | | // MyLog.d("progressRequestBody>>>" + "bytesWritten:" + bytesWritten + ">>>contentLength: " + contentLength ); |
| | | // 更新进度条或进行其他操作 |
| | | int progress = (int) (100 * bytesWritten / contentLength); |
| | | // 例如:更新 ProgressBar 或显示进度 |
| | | runOnUiThread(() -> { |
| | | uplodData.setProgress(progress); |
| | | // 进度条更新代码 |
| | | if (adapter != null && uplodData.getAdapterPosition() != -1) { |
| | | adapter.updateProgress(uplodData); |
| | | } |
| | | MyLog.d("progressRequestBody>>>" + "adapterPosition:" + uplodData.getAdapterPosition() + ">>>上传进度: " + progress + "%"); |
| | | }); |
| | | |
| | | }); |
| | | MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), progressRequestBody); |
| | | RequestBody description = RequestBody.create(MediaType.parse("text/plain"), "file description"); |
| | | Call<BaseResponse> uploadFile = apiService.uploadFile(body, description); |
| | | uplodData.setThisCall(uploadFile); |
| | | uploadFile.enqueue(new Callback<BaseResponse>() { |
| | | @Override |
| | | public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) { |
| | | if (response.body() != null) { |
| | | if (response.body().isSuccess()) { |
| | | uplodData.setState(1); |
| | | uplodData.setPostId(((LinkedTreeMap) response.body().getContent()).get("id").toString()); |
| | | uplodData.setWebPath(((LinkedTreeMap) response.body().getContent()).get("webPath").toString()); |
| | | listener.onBack(uplodData); |
| | | } else { |
| | | if (uplodData.getNumber() <= uplodFilerepeatSize) { |
| | | uplodData.setNumber(uplodData.getNumber() + 1); |
| | | uplodData.setState(2); |
| | | uploadFile(context, uplodData, listener, adapter); |
| | | } else { |
| | | if (!TextUtils.isEmpty(uplodData.getFilePath())) { |
| | | MyLog.d("progressRequestBody_uploadFile>>>" + "path:" + uplodData.getFilePath() + ">>>uploadType:" + uplodData.getUploadType()); |
| | | uplodData.setState(STATE_UPDING); |
| | | File file = new File(uplodData.getFilePath()); |
| | | RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file); |
| | | ProgressRequestBody progressRequestBody = new ProgressRequestBody(requestBody, (bytesWritten, contentLength, done) -> { |
| | | // MyLog.d("progressRequestBody>>>" + "bytesWritten:" + bytesWritten + ">>>contentLength: " + contentLength ); |
| | | // 更新进度条或进行其他操作 |
| | | int progress = (int) (100 * bytesWritten / contentLength); |
| | | // 例如:更新 ProgressBar 或显示进度 |
| | | runOnUiThread(() -> { |
| | | if (progress <5) { |
| | | uplodData.setProgress(0); |
| | | } |
| | | // 进度条更新代码 |
| | | if (adapter != null && uplodData.getAdapterPosition() != -1) { |
| | | //优化不频繁更新进度 |
| | | if (progress - uplodData.getProgress() > 5 || progress == 100) { |
| | | uplodData.setProgress(progress); |
| | | adapter.updateProgress(uplodData); |
| | | |
| | | } |
| | | |
| | | } |
| | | MyLog.d("progressRequestBodyHolder>>>path" + "adapterPosition:" + uplodData.getAdapterPosition() + ">>>上传进度: " + progress + "%"); |
| | | }); |
| | | |
| | | }); |
| | | MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), progressRequestBody); |
| | | RequestBody description = RequestBody.create(MediaType.parse("text/plain"), "file description"); |
| | | Call<BaseResponse> uploadFile = null; |
| | | switch (uplodData.getUploadType()) { |
| | | case UplodFileState.IMG_TYPE: |
| | | uploadFile = apiService.uploadImgFile(body, description); |
| | | break; |
| | | case UplodFileState.VIDEO_TYPE: |
| | | uploadFile = apiService.uploadVideoFile(body, description); |
| | | break; |
| | | default: |
| | | uploadFile = apiService.uploadImgFile(body, description); |
| | | break; |
| | | } |
| | | |
| | | uplodData.setThisCall(uploadFile); |
| | | uploadFile.enqueue(new Callback<BaseResponse>() { |
| | | @Override |
| | | public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) { |
| | | if (response.body() != null) { |
| | | if (response.body().isSuccess()) { |
| | | uplodData.setState(STATE_DONE); |
| | | uplodData.setPostId(((LinkedTreeMap) response.body().getContent()).get("id").toString()); |
| | | uplodData.setWebPath(((LinkedTreeMap) response.body().getContent()).get("webPath").toString()); |
| | | listener.onBack(uplodData); |
| | | } else { |
| | | MyLog.d("progressRequestBody>>>" + response.body().getMsg()); |
| | | if (uplodData.getNumber() <= uplodFilerepeatSize) { |
| | | uplodData.setNumber(uplodData.getNumber() + 1); |
| | | uplodData.setState(STATE_ERROR); |
| | | uploadFile(context, uplodData, listener, adapter); |
| | | } else { |
| | | uplodData.setState(STATE_ERROR); |
| | | uplodData.setNumber(0); |
| | | listener.onBack(uplodData); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onFailure(Call<BaseResponse> call, Throwable t) { |
| | | if (uplodData.getNumber() <= uplodFilerepeatSize) { |
| | | uplodData.setNumber(uplodData.getNumber() + 1); |
| | | uplodData.setState(2); |
| | | uploadFile(context, uplodData, listener, adapter); |
| | | } else { |
| | | listener.onBack(uplodData); |
| | | @Override |
| | | public void onFailure(Call<BaseResponse> call, Throwable t) { |
| | | MyLog.d("progressRequestBody>>>" + "onFailure:" + t.getMessage()); |
| | | if (!t.getMessage().equals("Canceled")) { |
| | | uplodData.setState(STATE_ERROR); |
| | | if (uplodData.getNumber() <= uplodFilerepeatSize) { |
| | | uplodData.setNumber(uplodData.getNumber() + 1); |
| | | uploadFile(context, uplodData, listener, adapter); |
| | | } else { |
| | | uplodData.setNumber(0); |
| | | listener.onBack(uplodData); |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | }); |
| | | |
| | | }); |
| | | } else { |
| | | uplodData.setState(STATE_ERROR); |
| | | listener.onBack(uplodData); |
| | | } |
| | | } |
| | | |
| | | |