管灌系统巡查员智能手机App
zuoxiao
2024-12-27 65a6bde49652558ada5daa15eca03d5061465a00
app/src/main/java/com/dayu/pipirrapp/net/ApiManager.java
@@ -5,10 +5,12 @@
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;
@@ -16,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;
@@ -61,7 +64,8 @@
    int uplodFilerepeatSize = 3;
    ApiService apiService;
    private CompositeDisposable compositeDisposable = new CompositeDisposable(); // 管理订阅事件
    // 管理订阅事件
    private CompositeDisposable compositeDisposable = new CompositeDisposable();
    /**
     * 初始化通信框架
@@ -136,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)) {
                                //当code为0000是跳转到登录界面
                                tem.setMsg("登录失效请重新登录");
                                redirectToLogin();
@@ -144,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) {
@@ -189,7 +198,6 @@
            return null;
        }
    }
    /**
@@ -248,8 +256,10 @@
            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 -> {
@@ -278,6 +288,10 @@
    }
//    public void uploadFile(final Context context, final UplodFileState uplodData, final UploadFileListener listener) {
//        uploadFile(context, uplodData, listener, null);
//    }
    /**
     * 上传文件
     *
@@ -285,22 +299,24 @@
     * @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");
@@ -312,14 +328,14 @@
                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);
                        }
@@ -332,7 +348,7 @@
                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);
                }
@@ -341,23 +357,6 @@
    }
    /**
     * 判断当前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;
    }
    /**
     * 当code为“0000”时跳转到登录界面