|  |  |  | 
|---|
|  |  |  | package com.dayu.pipirrapp.net; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import android.content.Context; | 
|---|
|  |  |  | import android.net.ConnectivityManager; | 
|---|
|  |  |  | import android.net.NetworkCapabilities; | 
|---|
|  |  |  | import android.net.NetworkInfo; | 
|---|
|  |  |  | import android.os.Build; | 
|---|
|  |  |  | import android.util.Log; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.dayu.pipirrapp.MyApplication; | 
|---|
|  |  |  | 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.BaseProgressSubscriber; | 
|---|
|  |  |  | 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.UploadFileListener; | 
|---|
|  |  |  | import com.dayu.pipirrapp.utils.MapJpgUtils; | 
|---|
|  |  |  | import com.dayu.pipirrapp.utils.MyJsonParser; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | import io.reactivex.rxjava3.disposables.CompositeDisposable; | 
|---|
|  |  |  | import io.reactivex.rxjava3.functions.Function; | 
|---|
|  |  |  | import io.reactivex.rxjava3.schedulers.Schedulers; | 
|---|
|  |  |  | import okhttp3.MediaType; | 
|---|
|  |  |  | import okhttp3.MultipartBody; | 
|---|
|  |  |  | import okhttp3.RequestBody; | 
|---|
|  |  |  | import okhttp3.ResponseBody; | 
|---|
|  |  |  | import retrofit2.Call; | 
|---|
|  |  |  | import retrofit2.Callback; | 
|---|
|  |  |  | import retrofit2.Response; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 发送请求 | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (isGet) { | 
|---|
|  |  |  | if (params == null) { | 
|---|
|  |  |  | observable = apiService.requestGet(path); | 
|---|
|  |  |  | observable = apiService.requestGet(path, MyApplication.myApplication.token); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | observable = apiService.requestGet(path, params); | 
|---|
|  |  |  | observable = apiService.requestGet(path, params, MyApplication.myApplication.token); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | observable = apiService.requestPost(path, params); | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取验证码 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param params | 
|---|
|  |  |  | * @param listener | 
|---|
|  |  |  | * @param <T> | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public <T> void getCode( Map<String, Object> params, CodeListener listener) { | 
|---|
|  |  |  | Observable observable; | 
|---|
|  |  |  | observable = apiService.getCode(params); | 
|---|
|  |  |  | observable.subscribeOn(Schedulers.io()) | 
|---|
|  |  |  | .unsubscribeOn(Schedulers.newThread()) | 
|---|
|  |  |  | .observeOn(AndroidSchedulers.mainThread()) | 
|---|
|  |  |  | .subscribe(responseBody -> { | 
|---|
|  |  |  | listener.onNext((CodeResult) responseBody); | 
|---|
|  |  |  | }, throwable -> { | 
|---|
|  |  |  | listener.error((Throwable) throwable); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | public <T> void getCode(Map<String, Object> params, CodeListener listener) { | 
|---|
|  |  |  | if (isNetworkAvailable(MyApplication.myApplication)) { | 
|---|
|  |  |  | Observable observable; | 
|---|
|  |  |  | observable = apiService.getCode(params); | 
|---|
|  |  |  | observable.subscribeOn(Schedulers.io()) | 
|---|
|  |  |  | .unsubscribeOn(Schedulers.newThread()) | 
|---|
|  |  |  | .observeOn(AndroidSchedulers.mainThread()) | 
|---|
|  |  |  | .subscribe(responseBody -> { | 
|---|
|  |  |  | listener.onNext((CodeResult) responseBody); | 
|---|
|  |  |  | }, throwable -> { | 
|---|
|  |  |  | listener.error((Throwable) throwable); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | listener.onNext(null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 判断当前是否有网络 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @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 listener | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void uploadFile(final Context context, final UplodFileState file, 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>() { | 
|---|
|  |  |  | @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); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | if (file.getNumber() <= uplodFilerepeatSize) { | 
|---|
|  |  |  | file.setNumber(file.getNumber() + 1); | 
|---|
|  |  |  | file.setState(2); | 
|---|
|  |  |  | uploadFile(context, file, listener); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | listener.onBack(file); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @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); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | listener.onBack(file); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|