| package com.dayu.qiheonlinelibrary.net; | 
|   | 
| import android.content.Context; | 
| import android.text.TextUtils; | 
|   | 
| import com.dayu.baselibrary.business.BusinessProvider; | 
| import com.dayu.baselibrary.net.subscribers.BaseProgressSubscriber; | 
| import com.dayu.baselibrary.net.subscribers.ProgressSubscriber; | 
| import com.dayu.baselibrary.net.subscribers.SubscriberListener; | 
| import com.dayu.qiheonlinelibrary.bean.BaseResult; | 
|   | 
| import com.dayu.qiheonlinelibrary.utils.MyJsonParser; | 
|   | 
| import java.util.List; | 
| import java.util.Map; | 
|   | 
| import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; | 
| import io.reactivex.rxjava3.core.Observable; | 
| import io.reactivex.rxjava3.functions.Function; | 
| import io.reactivex.rxjava3.schedulers.Schedulers; | 
|   | 
|   | 
| /** | 
|  * Copyright (C), 2023, | 
|  * Author: zuot | 
|  * Date: 2023-04-12 9:11 | 
|  * Description: | 
|  */ | 
| public class ApiManager { | 
|   | 
|     static ApiManager apiManager; | 
|   | 
|     ApiService apiService; | 
|   | 
|     public static void init() { | 
|         if (apiManager == null) { | 
|             apiManager = new ApiManager(); | 
|         } | 
|     } | 
|   | 
|     public ApiManager() { | 
|         apiService = RetrofitClient.getInstance().getApiService(); | 
|     } | 
|   | 
|     public static ApiManager getInstance() { | 
|         return apiManager; | 
|     } | 
|   | 
|   | 
|     public <T> void requestPostLoading(final Context context, final String path, final Class<T> tClass, final Map<String, Object> params, final SubscriberListener listener) { | 
|         request(context, false, path, false, tClass, params, listener); | 
|     } | 
|   | 
|     public <T> void requestPostHideLoading(final Context context, final String path, final Class<T> tClass, final Map<String, Object> params, final SubscriberListener listener) { | 
|         request(context, true, path, false, tClass, params, listener); | 
|     } | 
|   | 
|     public <T> void requestPost(final Context context, final String path, final Class<T> tClass, final Map<String, Object> params, final SubscriberListener listener) { | 
|         request(context, false, path, false, tClass, params, listener); | 
|     } | 
|   | 
|     /** | 
|      * 发送请求 | 
|      * | 
|      * @param context | 
|      * @param hideLoading 是否显示加载框  false:显示  true:隐藏 | 
|      * @param path        请求路径,在UrlConfig中定义 | 
|      * @param isGet       是否是Get请求 true:get 请求 | 
|      * @param tClass      对应的数据类型 | 
|      * @param params      Post请求时,对应的参数 | 
|      * @param listener    回调请求 | 
|      * @param <T> | 
|      */ | 
|     public <T> void request(final Context context, final boolean hideLoading, final String path, final boolean isGet, final Class<T> tClass, final Map<String, Object> params, final SubscriberListener listener) { | 
|         Observable observable; | 
|         BaseProgressSubscriber<?> mySubscriber; | 
|   | 
|   | 
|         if (isGet) { | 
|             if (params == null) { | 
|                 observable = apiService.requestGet(path); | 
|             } else { | 
|   | 
|                 observable = apiService.requestGet(path, params); | 
|             } | 
|         } else { | 
|             if (params != null) { | 
|                 observable = apiService.requestPost(path, params); | 
|             } else { | 
|                 observable = apiService.requestPost(path); | 
|             } | 
|         } | 
|   | 
|         mySubscriber = new ProgressSubscriber(context, hideLoading, listener); | 
|         observable.subscribeOn(Schedulers.io()). | 
|                 map(new Function<Object, BaseResponse<T>>() { | 
|                     @Override | 
|                     public BaseResponse<T> apply(Object o) { | 
|                         if (o instanceof BaseResponse) { | 
|                             BaseResponse tem = (BaseResponse) o; | 
|                             BaseResponse<T> response = new BaseResponse<>(); | 
|                             //未登录或登录超时,请重新登录 | 
|                             if (tem.getCode() == 100401) { | 
|                                 if (BusinessProvider.getBusinessProvider() != null) { | 
|                                     BusinessProvider.getBusinessProvider().startLoginNavigotor.navigateToLogin(context); | 
|                                 } | 
|                             } | 
|                             response.setCode(tem.getCode()); | 
|                             response.setMsg(tem.getMsg()); | 
|                             if (tClass != null) { | 
|                                 if (TextUtils.isEmpty(tem.getData().toString()) && BaseResult.class.isAssignableFrom(tClass)) { | 
|                                     response.setData(null); | 
|                                     return response; | 
|                                 } | 
|                                 if (tem.getData() instanceof Map) { | 
|                                     try { | 
| //                                    response.setData(MyJsonParser.getBeanFromMap((Map<String, Object>) tem.getData(), tClass)); | 
|                                         String jsonData = MyJsonParser.getJsontoMap((Map) tem.getData()); | 
|                                         response.setData(MyJsonParser.getBeanFromJson(jsonData, tClass)); | 
|                                     } catch (Exception e) { | 
|                                         e.printStackTrace(); | 
|                                     } | 
|                                 } else if (tem.getData() instanceof List) { | 
|                                     try { | 
|                                         response.setData((T) MyJsonParser.getListByJson(MyJsonParser.getJsonbyList((List) tem.getData()), tClass)); | 
|                                     } catch (Exception e) { | 
|                                         e.printStackTrace(); | 
|                                     } | 
|                                 } else if (tem.getData() instanceof Integer) { | 
|                                     response.setData((T) tem.getData()); | 
|                                 } else if (tem.getData() instanceof Boolean) { | 
|                                     response.setData((T) tem.getData()); | 
|                                 } | 
|                                 if (tClass.getName() instanceof String && tem.getData() instanceof String) { | 
|                                     try { | 
|                                         response.setData((T) tem.getData()); | 
|                                     } catch (Exception e) { | 
|                                         e.printStackTrace(); | 
|                                     } | 
|                                 } | 
|                                 return response; | 
|                             } | 
|                         } | 
|   | 
|                         return null; | 
|                     } | 
|                 }) | 
|                 .unsubscribeOn(Schedulers.newThread()) | 
|                 .observeOn(AndroidSchedulers.mainThread()) | 
|                 .subscribe(mySubscriber); | 
|   | 
|     } | 
|   | 
|   | 
| } |