|  |  |  | 
|---|
|  |  |  | package com.dayu.pipirrapp.net; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import static com.luck.picture.lib.thread.PictureThreadUtils.runOnUiThread; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import android.content.Context; | 
|---|
|  |  |  | import android.net.ConnectivityManager; | 
|---|
|  |  |  | import android.net.NetworkCapabilities; | 
|---|
|  |  |  | import android.net.NetworkInfo; | 
|---|
|  |  |  | import android.os.Build; | 
|---|
|  |  |  | import android.content.Intent; | 
|---|
|  |  |  | import android.util.Log; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.dayu.pipirrapp.MyApplication; | 
|---|
|  |  |  | import com.dayu.pipirrapp.activity.LoginActivity; | 
|---|
|  |  |  | 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.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.MapJpgUtils; | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | 
|---|
|  |  |  | public <T> void requestGet(final Context context, final String path, final Class<T> tClass, final Map<String, Object> params, final SubscriberListener listener) { | 
|---|
|  |  |  | request(context, false, path, true, tClass, params, listener); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public <T> void requestGetHideLoading(final Context context, final String path, final Class<T> tClass, final Map<String, Object> params, final SubscriberListener listener) { | 
|---|
|  |  |  | request(context, true, path, true, tClass, params, listener); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | public BaseResponse<T> apply(Object o) { | 
|---|
|  |  |  | if (o instanceof BaseResponse) { | 
|---|
|  |  |  | BaseResponse tem = (BaseResponse) o; | 
|---|
|  |  |  | if (tem.getCode().equals("0000")) { | 
|---|
|  |  |  | //当code为0000是跳转到登录界面 | 
|---|
|  |  |  | tem.setMsg("登录失效请重新登录"); | 
|---|
|  |  |  | redirectToLogin(); | 
|---|
|  |  |  | return tem; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | BaseResponse<T> response = new BaseResponse<>(); | 
|---|
|  |  |  | response.setCode(tem.getCode()); | 
|---|
|  |  |  | response.setMsg(tem.getMsg()); | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param <T> | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public <T> void getCode(Map<String, Object> params, CodeListener listener) { | 
|---|
|  |  |  | if (isNetworkAvailable(MyApplication.myApplication)) { | 
|---|
|  |  |  | if (NetUtils.isNetworkAvailable(MyApplication.myApplication)) { | 
|---|
|  |  |  | Observable observable; | 
|---|
|  |  |  | observable = apiService.getCode(params); | 
|---|
|  |  |  | observable.subscribeOn(Schedulers.io()) | 
|---|
|  |  |  | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param urlPath | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void donwLoadTile(String urlPath) { | 
|---|
|  |  |  | Observable<ResponseBody> observable; | 
|---|
|  |  |  | observable = apiService.downloadTile(urlPath); | 
|---|
|  |  |  | 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 线程 | 
|---|
|  |  |  | .subscribe( | 
|---|
|  |  |  | responseBody -> { | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //        compositeDisposable.add( | 
|---|
|  |  |  | observable | 
|---|
|  |  |  | .subscribeOn(Schedulers.io())  // 网络请求在 I/O 线程中进行 | 
|---|
|  |  |  | .observeOn(Schedulers.io())    // 回调处理也在 I/O 线程 | 
|---|
|  |  |  | .subscribe( | 
|---|
|  |  |  | responseBody -> { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | boolean success = false; | 
|---|
|  |  |  | if (!MapJpgUtils.getInsatance().isHasFiles(urlPath)) { | 
|---|
|  |  |  | success = MapJpgUtils.getInsatance().saveTileToCache(urlPath, responseBody); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (success) { | 
|---|
|  |  |  | Log.d(TAG, "Download success for tile: " + urlPath); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | Log.e(TAG, "Failed to save tile to disk: " + urlPath); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | throwable -> Log.e(TAG, "Download failed for tile: " + urlPath) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | //        ); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | boolean success = false; | 
|---|
|  |  |  | if (!MapJpgUtils.getInsatance().isHasFiles(urlPath)) { | 
|---|
|  |  |  | success = MapJpgUtils.getInsatance().saveTileToCache(urlPath, responseBody); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (success) { | 
|---|
|  |  |  | Log.d(TAG, "Download success for tile: " + urlPath); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | Log.e(TAG, "Failed to save tile to disk: " + urlPath); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | throwable -> Log.e(TAG, "Download failed for tile: " + urlPath) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | Log.e(TAG, "没有网络不下载: " + urlPath); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 停止线程池,释放资源 | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 判断当前是否有网络 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param context | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean isNetworkAvailable(Context context) { | 
|---|
|  |  |  | ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (connectivityManager != null) { | 
|---|
|  |  |  | // 对于 Android 版本大于等于 Android Q (API level 29) | 
|---|
|  |  |  | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | 
|---|
|  |  |  | NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork()); | 
|---|
|  |  |  | if (capabilities != null) { | 
|---|
|  |  |  | // 检查网络是否有传输能力(包括 Wi-Fi 和移动数据) | 
|---|
|  |  |  | if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | // 对于 Android Q 以下版本 | 
|---|
|  |  |  | NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); | 
|---|
|  |  |  | if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) { | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return false;  // 无网络连接 | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 上传文件 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param context | 
|---|
|  |  |  | * @param file | 
|---|
|  |  |  | * @param uplodData | 
|---|
|  |  |  | * @param listener | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void uploadFile(final Context context, final UplodFileState file, final UploadFileListener listener) { | 
|---|
|  |  |  | public void uploadFile(final Context context, final UplodFileState uplodData, final UploadFileListener listener) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 创建 RequestBody,用于封装构建RequestBody | 
|---|
|  |  |  | RequestBody requestFile = | 
|---|
|  |  |  | RequestBody.create(MediaType.parse("multipart/form-data"), file.getFile()); | 
|---|
|  |  |  | // MultipartBody.Part  和后端约定好Key,这里的partName是用image | 
|---|
|  |  |  | MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getFile().getName(), requestFile); | 
|---|
|  |  |  | // 添加描述 | 
|---|
|  |  |  | String descriptionString = "hello, 这是文件描述"; | 
|---|
|  |  |  | RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"), descriptionString); | 
|---|
|  |  |  | apiService.uploadFile(description, body).enqueue(new Callback<BaseResponse>() { | 
|---|
|  |  |  | 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 + "%"); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | 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>() { | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) { | 
|---|
|  |  |  | if (response.body() != null) { | 
|---|
|  |  |  | if (response.body().isSuccess()) { | 
|---|
|  |  |  | file.setState(1); | 
|---|
|  |  |  | file.setUrl(response.body().getMsg()); | 
|---|
|  |  |  | listener.onBack(file); | 
|---|
|  |  |  | uplodData.setState(1); | 
|---|
|  |  |  | uplodData.setId(((LinkedTreeMap) response.body().getContent()).get("id").toString()); | 
|---|
|  |  |  | uplodData.setWebPath(((LinkedTreeMap) response.body().getContent()).get("webPath").toString()); | 
|---|
|  |  |  | listener.onBack(uplodData); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | if (file.getNumber() <= uplodFilerepeatSize) { | 
|---|
|  |  |  | file.setNumber(file.getNumber() + 1); | 
|---|
|  |  |  | file.setState(2); | 
|---|
|  |  |  | uploadFile(context, file, listener); | 
|---|
|  |  |  | if (uplodData.getNumber() <= uplodFilerepeatSize) { | 
|---|
|  |  |  | uplodData.setNumber(uplodData.getNumber() + 1); | 
|---|
|  |  |  | uplodData.setState(2); | 
|---|
|  |  |  | uploadFile(context, uplodData, listener); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | listener.onBack(file); | 
|---|
|  |  |  | listener.onBack(uplodData); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void onFailure(Call<BaseResponse> call, Throwable t) { | 
|---|
|  |  |  | if (file.getNumber() <= uplodFilerepeatSize) { | 
|---|
|  |  |  | file.setNumber(file.getNumber() + 1); | 
|---|
|  |  |  | file.setState(2); | 
|---|
|  |  |  | uploadFile(context, file, listener); | 
|---|
|  |  |  | if (uplodData.getNumber() <= uplodFilerepeatSize) { | 
|---|
|  |  |  | uplodData.setNumber(uplodData.getNumber() + 1); | 
|---|
|  |  |  | uplodData.setState(2); | 
|---|
|  |  |  | uploadFile(context, uplodData, listener); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | listener.onBack(file); | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 当code为“0000”时跳转到登录界面 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private void redirectToLogin() { | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Context context = MyApplication.myApplication.getApplicationContext(); | 
|---|
|  |  |  | CleanDataUtils.cleanUserData(context); | 
|---|
|  |  |  | Intent intent = new Intent(context, LoginActivity.class); // Assuming LoginActivity is your login screen | 
|---|
|  |  |  | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // Clear task stack | 
|---|
|  |  |  | context.startActivity(intent); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|