refactor(generallibrary):重构网络请求模块
- 重构 ApiManager 类,优化请求流程和错误处理
- 新增 MyJsonParser 工具类,用于 JSON 解析
- 更新 RetrofitClient配置,添加日志拦截器和自定义拦截器
- 修改 SearchUserActivity,实现实际的搜索逻辑
- 新增 SearchUserBeanRequest 数据模型类
1 文件已重命名
22个文件已修改
4个文件已添加
| | |
| | | <?xml version="1.0" encoding="utf-8"?> |
| | | <manifest xmlns:android="http://schemas.android.com/apk/res/android" |
| | | package="com.dayu.henanlibrary"> |
| | | > |
| | | |
| | | <uses-permission android:name="android.permission.NFC" /> |
| | | <!--用于访问网络,网络定位需要上网--> |
| | |
| | | apply plugin: 'com.android.library' |
| | | |
| | | android { |
| | | compileSdk 33 |
| | | namespace 'com.dayu.baselibrary' |
| | | compileSdk 34 |
| | | defaultConfig { |
| | | minSdk 23 |
| | | targetSdk 26 |
| | |
| | | viewBinding { |
| | | enabled = true; |
| | | } |
| | | sourceSets { |
| | | main { |
| | | aidl.srcDirs = ['src/main/aidl'] |
| | | } |
| | | } |
| | | } |
| | | |
| | | dependencies { |
| | | |
| | | |
| | | compileOnly fileTree(include: ['*.jar'], dir: 'exlibs') |
| | | implementation fileTree(include: ['*.jar'], dir: 'exlibs') |
| | | |
| | | implementation 'com.android.support:appcompat-v7:28.0.0' |
| | | implementation 'com.android.support.constraint:constraint-layout:1.1.3' |
| | |
| | | <?xml version="1.0" encoding="utf-8"?> |
| | | <manifest xmlns:android="http://schemas.android.com/apk/res/android" |
| | | package="com.dayu.baselibrary"> |
| | | |
| | | > |
| | | <queries> |
| | | <package android:name="woyou.aidlservice.jiuiv5" /> |
| | | </queries> |
| | | <application |
| | | android:allowBackup="true" |
| | | android:supportsRtl="true" |
| | |
| | | import woyou.aidlservice.jiuiv5.ICallback; |
| | | import woyou.aidlservice.jiuiv5.IWoyouService; |
| | | |
| | | |
| | | |
| | | /** |
| | | * 商米打印模块相关代码 |
| | | */ |
| | |
| | | // Top-level build file where you can add configuration options common to all sub-projects/modules. |
| | | buildscript { |
| | | ext { |
| | | agp_version = '8.2.0' |
| | | agp_version1 = '7.2.2' |
| | | } |
| | | }// Top-level build file where you can add configuration options common to all sub-projects/modules. |
| | | plugins { |
| | | id 'com.android.application' version '7.2.2' apply false |
| | | id 'com.android.library' version '7.2.2' apply false |
| | |
| | | apply plugin: 'com.android.library' |
| | | android { |
| | | compileSdkVersion 27 |
| | | namespace 'com.easysocket' |
| | | compileSdk 34 |
| | | //buildToolsVersion rootProject.ext.android.buildToolsVersion |
| | | |
| | | defaultConfig { |
| | |
| | | compileOnly 'com.tencent.bugly:crashreport:4.1.9.3' |
| | | } |
| | | |
| | | sourceCompatibility = "7" |
| | | targetCompatibility = "7" |
| | | |
| | |
| | | <manifest xmlns:android="http://schemas.android.com/apk/res/android" |
| | | package="com.socket" > |
| | | > |
| | | <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> |
| | | </manifest> |
| | |
| | | <?xml version="1.0" encoding="utf-8"?> |
| | | <manifest xmlns:android="http://schemas.android.com/apk/res/android" |
| | | package="com.dayu.henanlibrary"> |
| | | <manifest xmlns:android="http://schemas.android.com/apk/res/android"> |
| | | |
| | | <uses-permission android:name="android.permission.NFC" /> |
| | | <!--用于访问网络,网络定位需要上网--> |
| | |
| | | package com.dayu.general.activity |
| | | |
| | | import android.os.Bundle |
| | | import android.widget.Toast |
| | | import com.dayu.baselibrary.net.subscribers.SubscriberListener |
| | | import com.dayu.baselibrary.utils.ToastUtil |
| | | import com.dayu.baselibrary.view.TitleBar.ClickType_LEFT_IMAGE |
| | | import com.dayu.baselibrary.view.TitleBar.ClickType_RIGHT_IMAGE |
| | | import com.dayu.general.bean.net.SearchUserResult |
| | | import com.dayu.general.databinding.ActivitySearchUserGeBinding |
| | | import com.dayu.general.dialog.SearchDialog |
| | | import com.dayu.general.net.BaseResponse |
| | | import com.dayu.qiheonlinelibrary.net.ApiManager |
| | | |
| | | /** |
| | | * @author: zuo |
| | |
| | | searchDialog.show() |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 执行搜索逻辑 |
| | | * 这里只是一个示例方法,实际应用中需要根据具体需求实现 |
| | | */ |
| | | private fun performSearch(farmerId: String, farmerName: String, cardNumber: String) { |
| | | // 这里可以实现实际的搜索逻辑 |
| | | // 例如:调用API、查询数据库等 |
| | | |
| | | // 示例:构建搜索条件 |
| | | val searchConditions = mutableMapOf<String, String>() |
| | | |
| | | if (farmerId.isNotEmpty()) { |
| | | searchConditions["farmerId"] = farmerId |
| | | } |
| | | |
| | | if (farmerName.isNotEmpty()) { |
| | | searchConditions["farmerName"] = farmerName |
| | | } |
| | | |
| | | if (cardNumber.isNotEmpty()) { |
| | | searchConditions["cardNumber"] = cardNumber |
| | | } |
| | | |
| | | // 根据搜索条件执行搜索 |
| | | // 实际应用中,这里可能是网络请求或数据库查询 |
| | | // searchRepository.search(searchConditions) |
| | | // 调用实际的搜索API |
| | | searchUser(farmerId, farmerName, cardNumber) |
| | | } |
| | | |
| | | private fun searchUser(farmerId: String, farmerName: String, cardNumber: String) { |
| | | val map = mutableMapOf<String, String>() |
| | | |
| | | if (farmerId.isNotEmpty()) { |
| | | map["farmerId"] = farmerId |
| | | } |
| | | |
| | | if (farmerName.isNotEmpty()) { |
| | | map["farmerName"] = farmerName |
| | | } |
| | | |
| | | if (cardNumber.isNotEmpty()) { |
| | | map["cardNumber"] = cardNumber |
| | | } |
| | | |
| | | // 使用正确的类型参数 |
| | | ApiManager.getInstance().requestGetLoading( |
| | | this, |
| | | "searchUser", |
| | | SearchUserResult::class.java, |
| | | map, |
| | | object : SubscriberListener<BaseResponse<SearchUserResult>>() { |
| | | override fun onNext(t: BaseResponse<SearchUserResult>) { |
| | | if (t.success) { |
| | | // 处理搜索成功的情况 |
| | | val result = t.content |
| | | if (result != null) { |
| | | // 处理搜索结果 |
| | | handleSearchResult(result) |
| | | } else { |
| | | ToastUtil.show("未找到匹配的用户") |
| | | } |
| | | } else { |
| | | // 处理搜索失败的情况 |
| | | ToastUtil.show(t.msg) |
| | | } |
| | | } |
| | | |
| | | override fun onError(e: Throwable?) { |
| | | super.onError(e) |
| | | ToastUtil.show("搜索失败: ${e?.message ?: "未知错误"}") |
| | | } |
| | | } |
| | | ) |
| | | } |
| | | |
| | | /** |
| | | * 处理搜索结果 |
| | | */ |
| | | private fun handleSearchResult(result: SearchUserResult) { |
| | | // 在这里实现搜索结果的处理逻辑 |
| | | // 例如:更新UI显示搜索结果列表 |
| | | // 或者跳转到结果详情页面 |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.dayu.general.bean.net |
| | | |
| | | /** |
| | | * Description: |
| | | * Author: zuo |
| | | * Date: 2025/3/7 |
| | | */ |
| | | class BaseResult<T> { |
| | | } |
New file |
| | |
| | | package com.dayu.general.bean.net |
| | | |
| | | /** |
| | | * Description:搜索 |
| | | * Author: zuo |
| | | * Date: 2025/3/7 |
| | | */ |
| | | class SearchUserBeanRequest { |
| | | |
| | | |
| | | /** |
| | | * 农户姓名,支持模糊查询 |
| | | * 示例值: 研究院 |
| | | */ |
| | | var name: String? = null |
| | | |
| | | /** |
| | | * 农户编号,支持模糊查询 |
| | | * 示例值: 281012090001 |
| | | */ |
| | | var clientNum: String? = null |
| | | |
| | | /** |
| | | * 手机号,支持模糊查询 |
| | | * 示例值: 18633333333 |
| | | */ |
| | | var phone: String? = null |
| | | |
| | | /** |
| | | * 详细地址,支持模糊查询 |
| | | * 示例值: 蜀 |
| | | */ |
| | | var address: String? = null |
| | | |
| | | /** |
| | | * 所在村 |
| | | * 示例值: 2024062709530800002 |
| | | */ |
| | | var villageId: String? = null |
| | | |
| | | /** |
| | | * 所在片区 |
| | | */ |
| | | var blockId: String? = null |
| | | |
| | | /** |
| | | * 所在分水房 |
| | | */ |
| | | var divideId: String? = null |
| | | |
| | | /** |
| | | * 水卡编号,支持模糊查询 |
| | | * 示例值: 53232810100600001 |
| | | */ |
| | | var cardNum: String? = null |
| | | |
| | | /** |
| | | * 当前页码,必需 |
| | | * 示例值: 1 |
| | | */ |
| | | var pageCurr: Int = 1 |
| | | |
| | | /** |
| | | * 每页记录数,必需 |
| | | * 示例值: 10 |
| | | */ |
| | | var pageSize: Int = 10 |
| | | |
| | | |
| | | } |
New file |
| | |
| | | package com.dayu.general.bean.net |
| | | |
| | | /** |
| | | * Description: 搜索用户结果Bean |
| | | * Author: zuo |
| | | * Date: 2025/3/7 |
| | | */ |
| | | data class SearchUserResult( |
| | | val itemTotal: Int, // 总条目数 |
| | | val obj: List<UserInfo>, // 用户信息列表 |
| | | val pageCurr: Int, // 当前页码 |
| | | val pageSize: Int, // 每页大小 |
| | | val pageTotal: Int // 总页数 |
| | | ) { |
| | | /** |
| | | * 用户信息 |
| | | */ |
| | | data class UserInfo( |
| | | val address: String? = null, // 地址 |
| | | val cardCount: Int? = null, // 卡数量 |
| | | val clientNum: String? = null, // 客户编号 |
| | | val id: Int? = null, // ID |
| | | val idCard: String? = null, // 身份证 |
| | | val name: String? = null, // 姓名 |
| | | val operateDt: String? = null, // 操作日期 |
| | | val phone: String? = null // 电话 |
| | | ) |
| | | } |
| | |
| | | package com.dayu.general.net |
| | | 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 android.util.Log |
| | | import com.dayu.baselibrary.net.subscribers.ProgressSubscriber |
| | | import com.dayu.baselibrary.net.subscribers.SubscriberListener |
| | | import com.dayu.general.net.ApiService |
| | | import com.dayu.general.net.BaseResponse |
| | | import com.dayu.general.net.MyJsonParser |
| | | import com.dayu.general.net.NetConstans |
| | | import com.dayu.general.net.RetrofitClient |
| | | 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: |
| | | * Author: zuo |
| | | * Date: 2025-03-06 |
| | | */ |
| | | class ApiManager { |
| | | class ApiManager private constructor() { |
| | | |
| | | var apiService: ApiService? = null |
| | | companion object { |
| | | private const val TAG = "ApiManager" |
| | | |
| | | fun init() { |
| | | if (apiManager == null) { |
| | | apiManager = |
| | | com.dayu.qiheonlinelibrary.net.ApiManager() |
| | | @Volatile |
| | | private var apiManager: ApiManager? = null |
| | | |
| | | fun init() { |
| | | if (apiManager == null) { |
| | | synchronized(ApiManager::class) { |
| | | if (apiManager == null) { |
| | | apiManager = ApiManager() |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | fun getInstance(): ApiManager { |
| | | return apiManager ?: throw IllegalStateException("ApiManager not initialized") |
| | | } |
| | | } |
| | | |
| | | fun ApiManager() { |
| | | apiService = RetrofitClient.getInstance().getApiService() |
| | | private val apiService: ApiService = RetrofitClient.getInstance().getApiService() |
| | | |
| | | fun <T> requestGetLoading( |
| | | context: Context, |
| | | path: String, |
| | | tClass: Class<T>, |
| | | params: Map<String, Any>?, |
| | | listener: SubscriberListener<BaseResponse<T>> |
| | | ) { |
| | | request(context, false, path, true, tClass, params, listener) |
| | | } |
| | | |
| | | fun getInstance(): com.dayu.qiheonlinelibrary.net.ApiManager { |
| | | return apiManager |
| | | fun <T> requestGetHideLoading( |
| | | context: Context, |
| | | path: String, |
| | | tClass: Class<T>, |
| | | params: Map<String, Any>?, |
| | | listener: SubscriberListener<BaseResponse<T>> |
| | | ) { |
| | | request(context, true, path, true, tClass, params, listener) |
| | | } |
| | | |
| | | |
| | | fun <T> requestPostLoading( |
| | | context: Context?, |
| | | path: String?, |
| | | tClass: Class<T>?, |
| | | params: Map<String?, Any?>?, |
| | | listener: SubscriberListener<*>? |
| | | context: Context, |
| | | path: String, |
| | | tClass: Class<T>, |
| | | params: Map<String, Any>?, |
| | | listener: SubscriberListener<BaseResponse<T>> |
| | | ) { |
| | | request(context, false, path, false, tClass, params, listener) |
| | | } |
| | | |
| | | fun <T> requestPostHideLoading( |
| | | context: Context?, |
| | | path: String?, |
| | | tClass: Class<T>?, |
| | | params: Map<String?, Any?>?, |
| | | listener: SubscriberListener<*>? |
| | | context: Context, |
| | | path: String, |
| | | tClass: Class<T>, |
| | | params: Map<String, Any>?, |
| | | listener: SubscriberListener<BaseResponse<T>> |
| | | ) { |
| | | request(context, true, path, false, tClass, params, listener) |
| | | } |
| | | |
| | | fun <T> requestPost( |
| | | context: Context?, |
| | | path: String?, |
| | | tClass: Class<T>?, |
| | | params: Map<String?, Any?>?, |
| | | listener: SubscriberListener<*>? |
| | | context: Context, |
| | | path: String, |
| | | tClass: Class<T>, |
| | | params: Map<String, Any>?, |
| | | listener: SubscriberListener<BaseResponse<T>> |
| | | ) { |
| | | request(context, false, path, false, tClass, params, listener) |
| | | } |
| | |
| | | * @param params Post请求时,对应的参数 |
| | | * @param listener 回调请求 |
| | | * @param <T> |
| | | </T> */ |
| | | */ |
| | | fun <T> request( |
| | | context: Context?, |
| | | context: Context, |
| | | hideLoading: Boolean, |
| | | path: String?, |
| | | path: String, |
| | | isGet: Boolean, |
| | | tClass: Class<T>?, |
| | | params: Map<String?, Any?>?, |
| | | listener: SubscriberListener<*>? |
| | | tClass: Class<T>, |
| | | params: Map<String, Any>?, |
| | | listener: SubscriberListener<BaseResponse<T>> |
| | | ) { |
| | | val observable: Observable<*>=if (isGet) { |
| | | if (params == null) { |
| | | apiService.requestGet(path) |
| | | } else { |
| | | apiService.requestGet(path, params) |
| | | } |
| | | |
| | | val observable = if (isGet) { |
| | | if (params == null) apiService.requestGet(path) |
| | | else apiService.requestGet(path, params) |
| | | } else { |
| | | if (params != null) { |
| | | apiService.requestPost(path, params) |
| | |
| | | } |
| | | } |
| | | |
| | | val mySubscriber: BaseProgressSubscriber<*>=ProgressSubscriber<Any?>(context, hideLoading, listener) |
| | | observable.subscribeOn(Schedulers.io()).map |
| | | object : Function<Any?, com.dayu.qiheonlinelibrary.net.BaseResponse<T>?> { |
| | | override fun apply(o: Any): com.dayu.qiheonlinelibrary.net.BaseResponse<T> { |
| | | if (o is com.dayu.qiheonlinelibrary.net.BaseResponse) { |
| | | val tem: com.dayu.qiheonlinelibrary.net.BaseResponse = |
| | | o as com.dayu.qiheonlinelibrary.net.BaseResponse |
| | | val response: com.dayu.qiheonlinelibrary.net.BaseResponse<T> = |
| | | com.dayu.qiheonlinelibrary.net.BaseResponse<T>() |
| | | //未登录或登录超时,请重新登录 |
| | | 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.java.isAssignableFrom(tClass) |
| | | ) { |
| | | response.setData(null) |
| | | return response |
| | | } |
| | | if (tem.getData() is Map<*, *>) { |
| | | try { |
| | | // response.setData(MyJsonParser.getBeanFromMap((Map<String, Object>) tem.getData(), tClass)); |
| | | val jsonData: String = |
| | | MyJsonParser.getJsontoMap(tem.getData() as Map<*, *>) |
| | | response.setData(MyJsonParser.getBeanFromJson<T>(jsonData, tClass)) |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | } |
| | | } else if (tem.getData() is List<*>) { |
| | | try { |
| | | response.setData( |
| | | MyJsonParser.getListByJson<T>( |
| | | MyJsonParser.getJsonbyList<Any>( |
| | | tem.getData() as List<*> |
| | | ), tClass |
| | | ) as T |
| | | ) |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | } |
| | | } else if (tem.getData() is Int) { |
| | | response.setData(tem.getData() as T) |
| | | } else if (tem.getData() is Boolean) { |
| | | response.setData(tem.getData() as T) |
| | | } |
| | | if (tClass.name is String && tem.getData() is String) { |
| | | try { |
| | | response.setData(tem.getData() as T) |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | } |
| | | } |
| | | return response |
| | | } |
| | | } |
| | | // 使用BaseResponse<T>作为ProgressSubscriber的泛型类型 |
| | | val mySubscriber = ProgressSubscriber<BaseResponse<T>>(context, hideLoading, listener); |
| | | |
| | | return null |
| | | observable |
| | | ?.subscribeOn(Schedulers.io()) |
| | | ?.map(mapResponse(tClass)) |
| | | ?.unsubscribeOn(Schedulers.newThread()) |
| | | ?.observeOn(AndroidSchedulers.mainThread()) |
| | | ?.subscribe(mySubscriber) |
| | | } |
| | | |
| | | private fun <T> mapResponse(tClass: Class<T>): Function<Any, BaseResponse<T>> { |
| | | return Function { rawResponse -> |
| | | when (rawResponse) { |
| | | is BaseResponse<*> -> { |
| | | val temp = rawResponse |
| | | |
| | | // 创建新的响应对象并设置基本属性 |
| | | val response = BaseResponse<T>().apply { |
| | | code = temp.code |
| | | msg = temp.msg ?: "" |
| | | } |
| | | |
| | | // 处理token失效的情况 |
| | | if (temp.code == NetConstans.TOKEN_INVALID) { |
| | | // 可以在这里添加重定向到登录页面的逻辑 |
| | | // redirectToLogin() |
| | | return@Function response |
| | | } |
| | | |
| | | // 根据内容类型进行处理 |
| | | when (val content = temp.content) { |
| | | is Map<*, *> -> { |
| | | try { |
| | | val jsonData = MyJsonParser.getJsontoMap(content as Map<String, Any>) |
| | | response.content = MyJsonParser.getBeanFromJson(jsonData, tClass) |
| | | } catch (e: Exception) { |
| | | Log.e(TAG, "Error parsing map content", e) |
| | | } |
| | | } |
| | | is List<*> -> { |
| | | try { |
| | | @Suppress("UNCHECKED_CAST") |
| | | response.content = MyJsonParser.getListByJson( |
| | | MyJsonParser.getJsonbyList(content), |
| | | tClass |
| | | ) as T |
| | | } catch (e: Exception) { |
| | | Log.e(TAG, "Error parsing list content", e) |
| | | } |
| | | } |
| | | is Int, is String, is Boolean -> { |
| | | @Suppress("UNCHECKED_CAST") |
| | | response.content = content as T |
| | | } |
| | | } |
| | | |
| | | response |
| | | } |
| | | else -> throw IllegalArgumentException("Unexpected response type") |
| | | } |
| | | } |
| | | .unsubscribeOn(Schedulers.newThread()) |
| | | .observeOn(AndroidSchedulers.mainThread()) |
| | | .subscribe(mySubscriber) |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | } |
| | |
| | | |
| | | @POST |
| | | fun requestPost( |
| | | @Url url: String?, |
| | | @Body params: Map<String?, Any?>? |
| | | @Url url: String, |
| | | @Body params: Map<String, Any?>? |
| | | ): Observable<BaseResponse<Any?>>? |
| | | |
| | | @POST |
| | |
| | | |
| | | @GET("{url}") |
| | | fun requestGet( |
| | | @Path("url") url: String?, |
| | | @QueryMap params: Map<String?, Any?>? |
| | | @Path("url") url: String, |
| | | @QueryMap params: Map<String, Any?>? |
| | | ): Observable<BaseResponse<Any?>>? |
| | | |
| | | @GET("{url}") |
| | | fun requestGet(@Path("url") url: String?): Observable<BaseResponse<Any?>>? |
| | | fun requestGet(@Path("url") url: String): Observable<BaseResponse<Any?>>? |
| | | |
| | | } |
| | |
| | | * Date: 2025-03-06 |
| | | */ |
| | | class BaseResponse<T> { |
| | | var code: Int = 0 |
| | | var code: String = "" |
| | | var msg: String? = null |
| | | var content: T? = null |
| | | var success:Boolean = false |
File was renamed from generallibrary/src/main/java/com/dayu/general/net/MyInercepterApplication.kt |
| | |
| | | package com.dayu.general.net |
| | | |
| | | import android.text.TextUtils |
| | | import com.dayu.qiheonlinelibrary.QHOnLineApplication |
| | | import com.tencent.bugly.crashreport.CrashReport |
| | | import okhttp3.Interceptor |
| | | import okhttp3.Protocol |
| | |
| | | * Author: zuo |
| | | * Date: 2025-03-06 |
| | | */ |
| | | class MyInercepterApplication :Interceptor{ |
| | | class MyIntercepterApplication :Interceptor{ |
| | | |
| | | @Throws(IOException::class) |
| | | override fun intercept(chain: Interceptor.Chain): Response { |
| | |
| | | if (request.method == "GET") { |
| | | builder.url(request.url.toString()) |
| | | } else { |
| | | if (!TextUtils.isEmpty(QHOnLineApplication.getInstance().tokenStr)) { |
| | | builder.addHeader("Token-Cloud", QHOnLineApplication.getInstance().tokenStr) |
| | | } |
| | | |
| | | } |
| | | return builder.build() |
| | | } catch (e: Exception) { |
New file |
| | |
| | | package com.dayu.general.net |
| | | |
| | | import android.text.TextUtils |
| | | import com.google.gson.Gson |
| | | import com.google.gson.GsonBuilder |
| | | import com.google.gson.JsonParser |
| | | import com.google.gson.LongSerializationPolicy |
| | | import org.json.JSONArray |
| | | import org.json.JSONException |
| | | import org.json.JSONObject |
| | | import java.lang.reflect.ParameterizedType |
| | | import java.lang.reflect.Type |
| | | import java.util.* |
| | | |
| | | /** |
| | | * Description: JSON解析工具类 |
| | | * Author: zuo |
| | | * Date: 2025/3/7 |
| | | */ |
| | | class MyJsonParser { |
| | | |
| | | companion object { |
| | | /** |
| | | * 获取Json字符串中的属性值 |
| | | */ |
| | | @JvmStatic |
| | | @Throws(JSONException::class) |
| | | fun getPropertyFromJson(json: String, attrName: String): Any? { |
| | | return try { |
| | | val jsonObject = JSONObject(json) |
| | | jsonObject.get(attrName) |
| | | } catch (e: JSONException) { |
| | | null |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将对象转换成Json字符串 |
| | | */ |
| | | @JvmStatic |
| | | fun <T> bean2Json(t: T?): String { |
| | | return try { |
| | | if (t == null || TextUtils.isEmpty(t.toString())) return "" |
| | | Gson().toJson(t) |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | "" |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转换为T类型 |
| | | */ |
| | | @JvmStatic |
| | | fun <T> json2Bean(json: String, tClass: Class<T>): T? { |
| | | return try { |
| | | if (!TextUtils.isEmpty(json)) { |
| | | Gson().fromJson(json, tClass) |
| | | } else null |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | null |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将Json直接序列化为List |
| | | */ |
| | | @JvmStatic |
| | | fun <T> json2List(json: String, type: Type): List<T>? { |
| | | return try { |
| | | if (!TextUtils.isEmpty(json)) { |
| | | Gson().fromJson(json, type) |
| | | } else null |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | null |
| | | } |
| | | } |
| | | |
| | | private fun buildGson() = GsonBuilder().create() |
| | | |
| | | @JvmStatic |
| | | fun <T> getBeanFromJson(json: String, cl: Class<T>): T? { |
| | | return try { |
| | | buildGson().fromJson(json, cl) |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | null |
| | | } |
| | | } |
| | | |
| | | @JvmStatic |
| | | fun <T> getBeanFromJson(json: String, cl: Type): T? { |
| | | return try { |
| | | buildGson().fromJson(json, cl) |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | null |
| | | } |
| | | } |
| | | |
| | | @JvmStatic |
| | | fun <T> getBeanFromMap(map: Map<String, Any>, cl: Class<T>): T? { |
| | | return getBeanFromJson(getJsonFromMap(map), cl) |
| | | } |
| | | |
| | | @JvmStatic |
| | | fun getJsonFromMap(params: Map<String, Any>?): String { |
| | | if (params.isNullOrEmpty()) return "" |
| | | return getJSONObjectFromMap(params).toString() |
| | | } |
| | | |
| | | @JvmStatic |
| | | fun getJsontoMap(params: Map<*, *>): String { |
| | | return GsonBuilder() |
| | | .setLongSerializationPolicy(LongSerializationPolicy.STRING) |
| | | .serializeSpecialFloatingPointValues() |
| | | .create() |
| | | .toJson(params) |
| | | } |
| | | |
| | | @JvmStatic |
| | | fun <T> getJsonbyList(params: List<T>): String { |
| | | return Gson().toJson(params) |
| | | } |
| | | |
| | | @Suppress("UNCHECKED_CAST") |
| | | private fun getJSONObjectFromMap(params: Map<String, Any>): JSONObject { |
| | | val jsonObject = JSONObject() |
| | | for ((key, value) in params) { |
| | | try { |
| | | when (value) { |
| | | is List<*> -> { |
| | | if (value.isNotEmpty()) { |
| | | val array = JSONArray() |
| | | for (o in value) { |
| | | when (o) { |
| | | is Map<*, *> -> array.put(getJSONObjectFromMap(o as Map<String, Any>)) |
| | | is List<*> -> { |
| | | val array1 = JSONArray() |
| | | array1.put(getListFromJson(o.toString())) |
| | | array.put(array1) |
| | | } |
| | | else -> array.put(o) |
| | | } |
| | | } |
| | | jsonObject.put(key, array) |
| | | } |
| | | } |
| | | is Map<*, *> -> jsonObject.put(key, getJSONObjectFromMap(value as Map<String, Any>)) |
| | | else -> jsonObject.put(key, value) |
| | | } |
| | | } catch (e: JSONException) { |
| | | e.printStackTrace() |
| | | } |
| | | } |
| | | return jsonObject |
| | | } |
| | | |
| | | @JvmStatic |
| | | fun getMapFromJson(jsonStr: String?): HashMap<String, Any> { |
| | | val valueMap = LinkedHashMap<String, Any>() |
| | | if (jsonStr.isNullOrEmpty()) return valueMap |
| | | |
| | | try { |
| | | val jsonObject = JSONObject(jsonStr) |
| | | return getMapFromJsonObject(jsonObject) |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | } |
| | | return valueMap |
| | | } |
| | | |
| | | @JvmStatic |
| | | fun getListFromJsonArray(jsonArray: JSONArray): List<Any> { |
| | | val list = ArrayList<Any>() |
| | | try { |
| | | for (i in 0 until jsonArray.length()) { |
| | | when (val obj = jsonArray.get(i)) { |
| | | is JSONObject -> list.add(getMapFromJsonObject(obj)) |
| | | is JSONArray -> list.add(getListFromJsonArray(jsonArray)) |
| | | else -> { |
| | | if (obj != null && !TextUtils.isEmpty(obj.toString()) && obj.toString() != "null") { |
| | | list.add(obj) |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } catch (e: JSONException) { |
| | | e.printStackTrace() |
| | | } |
| | | return list |
| | | } |
| | | |
| | | @JvmStatic |
| | | fun getMapFromJsonObject(jsonObj: JSONObject): HashMap<String, Any> { |
| | | val map = LinkedHashMap<String, Any>() |
| | | val keys = jsonObj.keys() |
| | | try { |
| | | while (keys.hasNext()) { |
| | | val key = keys.next() |
| | | when (val obj = jsonObj.get(key)) { |
| | | is JSONArray -> map[key] = getListFromJsonArray(obj) |
| | | is JSONObject -> map[key] = getMapFromJsonObject(obj) |
| | | is Number -> map[key] = obj.toString() |
| | | else -> { |
| | | if (obj != null && !TextUtils.isEmpty(obj.toString()) && obj.toString() != "null") { |
| | | map[key] = obj |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } catch (e: JSONException) { |
| | | e.printStackTrace() |
| | | } |
| | | return map |
| | | } |
| | | |
| | | @JvmStatic |
| | | fun <T> getListFromJson(str: String, clazz: Class<T>): List<T>? { |
| | | return try { |
| | | val type = ParameterizedTypeImpl(clazz) |
| | | Gson().fromJson<List<T>>(str, type) |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | null |
| | | } |
| | | } |
| | | |
| | | @JvmStatic |
| | | fun <T> getListByJson(json: String, cls: Class<T>): List<T> { |
| | | val gson = Gson() |
| | | val list = ArrayList<T>() |
| | | val array = JsonParser().parse(json).asJsonArray |
| | | array.forEach { elem -> |
| | | list.add(gson.fromJson(elem, cls)) |
| | | } |
| | | return list |
| | | } |
| | | |
| | | @JvmStatic |
| | | fun getListFromJson(str: String): List<Any>? { |
| | | try { |
| | | val jsonArray = JSONArray(str) |
| | | return getListFromJsonArray(jsonArray) |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | } |
| | | return null |
| | | } |
| | | |
| | | private class ParameterizedTypeImpl(private val clazz: Class<*>) : ParameterizedType { |
| | | override fun getActualTypeArguments(): Array<Type> = arrayOf(clazz) |
| | | override fun getRawType(): Type = List::class.java |
| | | override fun getOwnerType(): Type? = null |
| | | } |
| | | } |
| | | } |
| | |
| | | companion object { |
| | | const val BASE_URL: String = "http://120.46.45.35:20081/api/sjgg/" |
| | | |
| | | const val TOKEN_INVALID: String = "0000" |
| | | } |
| | | |
| | | } |
| | |
| | | package com.dayu.general.net |
| | | |
| | | import com.dayu.baselibrary.BuildConfig |
| | | import com.dayu.qiheonlinelibrary.net.MyIntercepterApplication |
| | | import okhttp3.OkHttpClient |
| | | import okhttp3.logging.HttpLoggingInterceptor |
| | | import retrofit2.Retrofit |
| | |
| | | * Author: zuo |
| | | * Date: 2025-03-06 |
| | | */ |
| | | class RetrofitClient { |
| | | class RetrofitClient// 包含header、body数据 |
| | | // loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS); |
| | | //设置连接和读取时间 |
| | | //添加统一的header |
| | | //添加日志拦截器 |
| | | //添加数据请求统一处理拦截器 |
| | | () { |
| | | |
| | | |
| | | |
| | |
| | | val READ_TIME_OUT: Int = 10 |
| | | val CONNECT_TIME_OUT: Int = 10 |
| | | |
| | | private fun RetrofitClient() { |
| | | |
| | | init { |
| | | val loggingInterceptor = HttpLoggingInterceptor() |
| | | // 包含header、body数据 |
| | | loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY) |
| | | // loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS); |
| | | val builder: OkHttpClient.Builder = OkHttpClient().newBuilder() |
| | | //设置连接和读取时间 |
| | | builder.readTimeout(READ_TIME_OUT.toLong(), TimeUnit.SECONDS) |
| | | builder.connectTimeout(CONNECT_TIME_OUT.toLong(), TimeUnit.SECONDS) |
| | | builder.writeTimeout(CONNECT_TIME_OUT.toLong(), TimeUnit.SECONDS) |
| | | //添加统一的header |
| | | builder.addInterceptor(MyIntercepterApplication()) |
| | | //添加日志拦截器 |
| | | //添加数据请求统一处理拦截器 |
| | | if (BuildConfig.DEBUG) { |
| | | builder.addInterceptor(loggingInterceptor) |
| | | } |
| | | |
| | | val client: OkHttpClient = builder.build() |
| | | |
| | | retrofit = Retrofit.Builder() |
| | | .baseUrl(NetConstans.BASE_URL) |
| | | .addConverterFactory(GsonConverterFactory.create()) |
| | |
| | | .client(client).build() |
| | | } |
| | | |
| | | |
| | | @Synchronized |
| | | |
| | | |
| | |
| | | # Darwin, MinGW, and NonStop. |
| | | # |
| | | # (3) This script is generated from the Groovy template |
| | | # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt |
| | | # https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt |
| | | # within the Gradle project. |
| | | # |
| | | # You can find Gradle at https://github.com/gradle/gradle/. |
| | |
| | | esac |
| | | done |
| | | |
| | | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit |
| | | |
| | | APP_NAME="Gradle" |
| | | # This is normally unused |
| | | # shellcheck disable=SC2034 |
| | | APP_BASE_NAME=${0##*/} |
| | | |
| | | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. |
| | | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' |
| | | # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) |
| | | APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit |
| | | |
| | | # Use the maximum available, or set MAX_FD != -1 to use that value. |
| | | MAX_FD=maximum |
| | |
| | | fi |
| | | else |
| | | JAVACMD=java |
| | | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. |
| | | if ! command -v java >/dev/null 2>&1 |
| | | then |
| | | die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. |
| | | |
| | | Please set the JAVA_HOME variable in your environment to match the |
| | | location of your Java installation." |
| | | fi |
| | | fi |
| | | |
| | | # Increase the maximum file descriptors if we can. |
| | | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then |
| | | case $MAX_FD in #( |
| | | max*) |
| | | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. |
| | | # shellcheck disable=SC2039,SC3045 |
| | | MAX_FD=$( ulimit -H -n ) || |
| | | warn "Could not query maximum file descriptor limit" |
| | | esac |
| | | case $MAX_FD in #( |
| | | '' | soft) :;; #( |
| | | *) |
| | | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. |
| | | # shellcheck disable=SC2039,SC3045 |
| | | ulimit -n "$MAX_FD" || |
| | | warn "Could not set maximum file descriptor limit to $MAX_FD" |
| | | esac |
| | |
| | | done |
| | | fi |
| | | |
| | | # Collect all arguments for the java command; |
| | | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of |
| | | # shell script including quotes and variable substitutions, so put them in |
| | | # double quotes to make sure that they get re-expanded; and |
| | | # * put everything else in single quotes, so that it's not re-expanded. |
| | | |
| | | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. |
| | | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' |
| | | |
| | | # Collect all arguments for the java command: |
| | | # * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, |
| | | # and any embedded shellness will be escaped. |
| | | # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be |
| | | # treated as '${Hostname}' itself on the command line. |
| | | |
| | | set -- \ |
| | | "-Dorg.gradle.appname=$APP_BASE_NAME" \ |
| | |
| | | |
| | | set DIRNAME=%~dp0 |
| | | if "%DIRNAME%"=="" set DIRNAME=. |
| | | @rem This is normally unused |
| | | set APP_BASE_NAME=%~n0 |
| | | set APP_HOME=%DIRNAME% |
| | | |
| | |
| | | apply plugin: 'com.android.library' |
| | | android { |
| | | namespace 'com.dayu.henanlibrary' |
| | | compileSdk 33 |
| | | ndkPath 'D:\\android\\sdk\\ndk\\android-ndk-r21' |
| | | compileSdk 34 |
| | | ndkVersion '21.0.6113669' |
| | | defaultConfig { |
| | | minSdk 23 |
| | | targetSdk 26 |
| | |
| | | <?xml version="1.0" encoding="utf-8"?> |
| | | <manifest xmlns:android="http://schemas.android.com/apk/res/android" |
| | | package="com.dayu.henanlibrary"> |
| | | <manifest xmlns:android="http://schemas.android.com/apk/res/android"> |
| | | |
| | | <uses-permission android:name="android.permission.NFC" /> |
| | | <!--用于访问网络,网络定位需要上网--> |
| | |
| | | apply plugin: 'com.android.library' |
| | | |
| | | android { |
| | | compileSdkVersion 29 |
| | | namespace 'com.kernal.passportreader.sdk' |
| | | compileSdk 34 |
| | | defaultConfig { |
| | | minSdkVersion 14 |
| | | targetSdkVersion 29 |
| | |
| | | |
| | | |
| | | android { |
| | | compileSdkVersion 28 |
| | | |
| | | compileSdk 34 |
| | | namespace 'com.example.pickerviewlibrary' |
| | | defaultConfig { |
| | | minSdkVersion 22 |
| | | targetSdkVersion 28 |
| | |
| | | apply plugin: 'com.android.library' |
| | | android { |
| | | namespace 'com.dayu.qihealonelibrary' |
| | | compileSdk 33 |
| | | ndkPath 'D:\\android\\sdk\\ndk\\android-ndk-r21' |
| | | compileSdk 34 |
| | | ndkVersion '21.0.6113669' |
| | | defaultConfig { |
| | | minSdk 23 |
| | | targetSdk 33 |
| | |
| | | apply plugin: 'com.android.library' |
| | | android { |
| | | namespace 'com.dayu.qiheonlinelibrary' |
| | | compileSdk 33 |
| | | ndkPath 'D:\\android\\sdk\\ndk\\android-ndk-r21' |
| | | compileSdk 34 |
| | | ndkVersion '21.0.6113669' |
| | | defaultConfig { |
| | | minSdk 23 |
| | | targetSdk 33 |