From 65a6bde49652558ada5daa15eca03d5061465a00 Mon Sep 17 00:00:00 2001 From: zuoxiao <470321431@qq.com> Date: 星期五, 27 十二月 2024 18:00:46 +0800 Subject: [PATCH] 1.图片上传进度相关代码。 2.修复地图定位图标不居中问题。 --- app/src/main/java/com/dayu/pipirrapp/net/ApiManager.java | 86 +++++++++++++++++++++++-------------------- 1 files changed, 46 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/com/dayu/pipirrapp/net/ApiManager.java b/app/src/main/java/com/dayu/pipirrapp/net/ApiManager.java index fad2f78..d0d399a 100644 --- a/app/src/main/java/com/dayu/pipirrapp/net/ApiManager.java +++ b/app/src/main/java/com/dayu/pipirrapp/net/ApiManager.java @@ -2,12 +2,15 @@ import static com.luck.picture.lib.thread.PictureThreadUtils.runOnUiThread; +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; @@ -15,6 +18,7 @@ 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; @@ -60,7 +64,8 @@ int uplodFilerepeatSize = 3; ApiService apiService; - private CompositeDisposable compositeDisposable = new CompositeDisposable(); // 绠$悊璁㈤槄浜嬩欢 + // 绠$悊璁㈤槄浜嬩欢 + private CompositeDisposable compositeDisposable = new CompositeDisposable(); /** * 鍒濆鍖栭�氫俊妗嗘灦 @@ -135,7 +140,7 @@ public BaseResponse<T> apply(Object o) { if (o instanceof BaseResponse) { BaseResponse tem = (BaseResponse) o; - if (tem.getCode().equals("0000")) { + if (tem.getCode().equals(Constants.TOKEN_INVALID)) { //褰揷ode涓�0000鏄烦杞埌鐧诲綍鐣岄潰 tem.setMsg("鐧诲綍澶辨晥璇烽噸鏂扮櫥褰�"); redirectToLogin(); @@ -143,7 +148,12 @@ } 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) { @@ -188,6 +198,7 @@ return null; } } + /** * 鑾峰彇澶╂皵 @@ -239,13 +250,16 @@ * * @param urlPath */ + @SuppressLint("CheckResult") public void donwLoadTile(Context context, String urlPath) { if (NetUtils.isNetworkAvailable(context)) { Observable<ResponseBody> observable; observable = apiService.downloadTile(urlPath); observable - .subscribeOn(Schedulers.io()) // 缃戠粶璇锋眰鍦� I/O 绾跨▼涓繘琛� - .observeOn(Schedulers.io()) // 鍥炶皟澶勭悊涔熷湪 I/O 绾跨▼ + // 缃戠粶璇锋眰鍦� I/O 绾跨▼涓繘琛� + .subscribeOn(Schedulers.io()) + // 鍥炶皟澶勭悊涔熷湪 I/O 绾跨▼ + .observeOn(Schedulers.io()) .subscribe( responseBody -> { @@ -274,6 +288,10 @@ } +// public void uploadFile(final Context context, final UplodFileState uplodData, final UploadFileListener listener) { +// uploadFile(context, uplodData, listener, null); +// } + /** * 涓婁紶鏂囦欢 * @@ -281,39 +299,43 @@ * @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"); - apiService.uploadFile(body, description).enqueue(new Callback<BaseResponse>() { + 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.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); } @@ -326,31 +348,15 @@ 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); } } }); + } - /** - * 鍒ゆ柇褰撳墠code鏄惁鏄湭鐧诲綍鐘舵�� - * - * @param responseBody the response body in JSON format - * @return true if login is required, false otherwise - */ - private boolean isLoginRequired(String responseBody) { - try { - Gson gson = new Gson(); - Map<String, Object> responseMap = gson.fromJson(responseBody, Map.class); - String code = (String) responseMap.get("code"); - return "0000".equals(code); // Check if the code is not 0000 - } catch (Exception e) { - e.printStackTrace(); - } - return false; - } /** * 褰揷ode涓衡��0000鈥濇椂璺宠浆鍒扮櫥褰曠晫闈� -- Gitblit v1.8.0