From e7df063a027c0f066317da4437d01cf3f3bc8d31 Mon Sep 17 00:00:00 2001 From: zuoxiao <470321431@qq.com> Date: 星期三, 13 十一月 2024 17:28:31 +0800 Subject: [PATCH] 地图添加巡检定位功能 --- app/src/main/java/com/dayu/pipirrapp/net/ApiManager.java | 175 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 files changed, 128 insertions(+), 47 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 58a4dd0..0735ae5 100644 --- a/app/src/main/java/com/dayu/pipirrapp/net/ApiManager.java +++ b/app/src/main/java/com/dayu/pipirrapp/net/ApiManager.java @@ -1,10 +1,17 @@ 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.bean.WeatherResponse; +import com.dayu.pipirrapp.MyApplication; +import com.dayu.pipirrapp.bean.net.CodeResult; +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.utils.MapJpgUtils; @@ -72,6 +79,13 @@ request(context, false, path, false, tClass, params, listener); } + 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); + } + /** * 鍙戦�佽姹� * @@ -91,10 +105,9 @@ 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); @@ -111,33 +124,16 @@ response.setCode(tem.getCode()); response.setMsg(tem.getMsg()); - if (tClass != null && tem.getContent() instanceof Map) { - try { -// response.setData(MyJsonParser.getBeanFromMap((Map<String, Object>) tem.getData(), tClass)); - String jsonData = MyJsonParser.getJsontoMap((Map) tem.getContent()); - response.setContent(MyJsonParser.getBeanFromJson(jsonData, tClass)); - } catch (Exception e) { - e.printStackTrace(); - } - } else if (tClass != null && tem.getContent() instanceof List) { - try { - response.setContent((T) MyJsonParser.getListByJson(MyJsonParser.getJsonbyList((List) tem.getContent()), tClass)); - } catch (Exception e) { - e.printStackTrace(); - } - } else if (tClass != null && tem.getContent() instanceof Integer) { - response.setContent((T) tem.getContent()); - } - if (tClass != null && tClass.getName() instanceof String && tem.getContent() instanceof String) { - try { + if (tClass != null) { + if (tem.getContent() instanceof Map) { + response.setContent(parseMapContent(tem.getContent(), tClass)); + } else if (tem.getContent() instanceof List) { + response.setContent(parseListContent(tem.getContent(), tClass)); + } else if (tem.getContent() instanceof Integer || tem.getContent() instanceof String) { response.setContent((T) tem.getContent()); - } catch (Exception e) { - e.printStackTrace(); } } return response; - - } return null; @@ -149,6 +145,28 @@ } + + // 瑙f瀽 Map 绫诲瀷鐨勬暟鎹� + private <T> T parseMapContent(Object content, Class<T> tClass) { + try { + String jsonData = MyJsonParser.getJsontoMap((Map<String, Object>) content); + return MyJsonParser.getBeanFromJson(jsonData, tClass); + } catch (Exception e) { + Log.e(TAG, "Error parsing map content", e); + return null; + } + } + + // 瑙f瀽 List 绫诲瀷鐨勬暟鎹� + private <T> T parseListContent(Object content, Class<T> tClass) { + try { + String jsonData = MyJsonParser.getJsonbyList((List<?>) content); + return (T) MyJsonParser.getListByJson(jsonData, tClass); + } catch (Exception e) { + Log.e(TAG, "Error parsing list content", e); + return null; + } + } /** * 鑾峰彇澶╂皵 @@ -169,30 +187,62 @@ } - public void donwLoadTile(Context context, String urlPath) { + /** + * 鑾峰彇楠岃瘉鐮� + * + * @param params + * @param listener + * @param <T> + */ + 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 urlPath + */ + public void donwLoadTile(String urlPath) { Observable<ResponseBody> observable; observable = apiService.downloadTile(urlPath); - compositeDisposable.add( - observable - .subscribeOn(Schedulers.io()) // 缃戠粶璇锋眰鍦� I/O 绾跨▼涓繘琛� - .observeOn(Schedulers.io()) // 鍥炶皟澶勭悊涔熷湪 I/O 绾跨▼ - .subscribe( - responseBody -> { - boolean success = false; - if (!MapJpgUtils.getInsatance(context).isHasFiles(urlPath)) { - success = MapJpgUtils.getInsatance(context).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) - ) - ); +// 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) + ); +// ); } @@ -204,4 +254,35 @@ } } + /** + * 鍒ゆ柇褰撳墠鏄惁鏈夌綉缁� + * + * @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; // 鏃犵綉缁滆繛鎺� + } + } \ No newline at end of file -- Gitblit v1.8.0