| | |
| | | import android.annotation.SuppressLint; |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.text.TextUtils; |
| | | import android.util.Log; |
| | | |
| | | import com.dayu.pipirrapp.MyApplication; |
| | | import com.dayu.pipirrapp.activity.LoginActivity; |
| | | import com.dayu.pipirrapp.adapter.AddPictureAdapter; |
| | | import com.dayu.pipirrapp.bean.net.CodeResult; |
| | | import com.dayu.pipirrapp.bean.net.UplodFileState; |
| | | import com.dayu.pipirrapp.bean.net.WeatherResponse; |
| | |
| | | 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; |
| | |
| | | } |
| | | BaseResponse<T> response = new BaseResponse<>(); |
| | | response.setCode(tem.getCode()); |
| | | response.setMsg(tem.getMsg()); |
| | | if (!TextUtils.isEmpty(tem.getMsg())){ |
| | | response.setMsg(tem.getMsg()); |
| | | }else { |
| | | response.setMsg(""); |
| | | } |
| | | |
| | | |
| | | if (tClass != null) { |
| | | if (tem.getContent() instanceof Map) { |
| | |
| | | } |
| | | |
| | | |
| | | // public void uploadFile(final Context context, final UplodFileState uplodData, final UploadFileListener listener) { |
| | | // uploadFile(context, uplodData, listener, null); |
| | | // } |
| | | |
| | | /** |
| | | * 上传文件 |
| | | * |
| | |
| | | * @param uplodData |
| | | * @param listener |
| | | */ |
| | | public void uploadFile(final Context context, final UplodFileState uplodData, final UploadFileListener 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, new ProgressListener() { |
| | | @Override |
| | | public void onProgress(long bytesWritten, long contentLength, boolean done) { |
| | | // 更新进度条或进行其他操作 |
| | | int progress = (int) (100 * bytesWritten / contentLength); |
| | | // 例如:更新 ProgressBar 或显示进度 |
| | | runOnUiThread(() -> { |
| | | // 进度条更新代码 |
| | | // progressBar.setProgress(progress); |
| | | MyLog.d("progressRequestBody>>>" + "上传进度: " + progress + "%"); |
| | | }); |
| | | } |
| | | 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"); |
| | |
| | | if (response.body() != null) { |
| | | if (response.body().isSuccess()) { |
| | | uplodData.setState(1); |
| | | uplodData.setId(((LinkedTreeMap) response.body().getContent()).get("id").toString()); |
| | | 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); |
| | | uploadFile(context, uplodData, listener, adapter); |
| | | } else { |
| | | listener.onBack(uplodData); |
| | | } |
| | |
| | | if (uplodData.getNumber() <= uplodFilerepeatSize) { |
| | | uplodData.setNumber(uplodData.getNumber() + 1); |
| | | uplodData.setState(2); |
| | | uploadFile(context, uplodData, listener); |
| | | uploadFile(context, uplodData, listener, adapter); |
| | | } else { |
| | | listener.onBack(uplodData); |
| | | } |
| | |
| | | }); |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | /** |