├── entry ├── .gitignore ├── src │ ├── main │ │ ├── resources │ │ │ └── base │ │ │ │ ├── media │ │ │ │ └── icon.png │ │ │ │ └── element │ │ │ │ └── string.json │ │ ├── java │ │ │ └── com │ │ │ │ └── zzrv5 │ │ │ │ └── zzrhttp │ │ │ │ ├── MyApplication.java │ │ │ │ ├── MainAbility.java │ │ │ │ └── slice │ │ │ │ └── MainAbilitySlice.java │ │ └── config.json │ └── test │ │ └── java │ │ └── com │ │ └── zzrv5 │ │ └── zzrhttp │ │ └── MainAbilityTest.java └── build.gradle ├── mylibrary ├── .gitignore ├── src │ └── main │ │ ├── resources │ │ └── base │ │ │ ├── media │ │ │ └── icon.png │ │ │ └── element │ │ │ └── string.json │ │ ├── java │ │ └── com │ │ │ └── zzrv5 │ │ │ └── mylibrary │ │ │ ├── TextUtils.java │ │ │ ├── ZZRResponse.java │ │ │ ├── ZZRHttp.java │ │ │ ├── ZZRCallBack.java │ │ │ ├── ZZRRequest.java │ │ │ └── RequestUtil.java │ │ └── config.json └── build.gradle ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── .idea ├── misc.xml └── gradle.xml ├── gradle.properties ├── gradlew.bat ├── README.md └── gradlew /entry/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /mylibrary/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':entry', ':mylibrary' 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzrv5/ZZRHttp/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /entry/src/main/resources/base/media/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzrv5/ZZRHttp/HEAD/entry/src/main/resources/base/media/icon.png -------------------------------------------------------------------------------- /mylibrary/src/main/resources/base/media/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzrv5/ZZRHttp/HEAD/mylibrary/src/main/resources/base/media/icon.png -------------------------------------------------------------------------------- /mylibrary/src/main/resources/base/element/string.json: -------------------------------------------------------------------------------- 1 | { 2 | "string": [ 3 | { 4 | "name": "app_name", 5 | "value": "MyLibrary" 6 | } 7 | ] 8 | } 9 | -------------------------------------------------------------------------------- /entry/src/test/java/com/zzrv5/zzrhttp/MainAbilityTest.java: -------------------------------------------------------------------------------- 1 | package com.zzrv5.zzrhttp; 2 | 3 | import org.junit.Test; 4 | 5 | public class MainAbilityTest { 6 | @Test 7 | public void onStart() { 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /entry/src/main/resources/base/element/string.json: -------------------------------------------------------------------------------- 1 | { 2 | "string": [ 3 | { 4 | "name": "app_name", 5 | "value": "ZZRHttp" 6 | }, 7 | { 8 | "name": "mainability_description", 9 | "value": "hap sample empty page" 10 | } 11 | ] 12 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /entry/src/main/java/com/zzrv5/zzrhttp/MyApplication.java: -------------------------------------------------------------------------------- 1 | package com.zzrv5.zzrhttp; 2 | 3 | import ohos.aafwk.ability.AbilityPackage; 4 | 5 | public class MyApplication extends AbilityPackage { 6 | @Override 7 | public void onInitialize() { 8 | super.onInitialize(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /entry/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.huawei.ohos.hap' 2 | ohos { 3 | compileSdkVersion 3 4 | defaultConfig { 5 | compatibleSdkVersion 3 6 | } 7 | 8 | } 9 | 10 | dependencies { 11 | implementation fileTree(dir: 'libs', include: ['*.jar']) 12 | 13 | testCompile'junit:junit:4.12' 14 | implementation project(':mylibrary') 15 | 16 | } 17 | -------------------------------------------------------------------------------- /mylibrary/src/main/java/com/zzrv5/mylibrary/TextUtils.java: -------------------------------------------------------------------------------- 1 | package com.zzrv5.mylibrary; 2 | 3 | /** 4 | * TextUtils 5 | * @author ZZR ,program.java@qq.com 6 | * @version 1.0.0 7 | * @since 2020-10-3 8 | */ 9 | public class TextUtils { 10 | 11 | public static boolean isEmpty(CharSequence str) { 12 | return str == null || str.length() == 0; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /entry/src/main/java/com/zzrv5/zzrhttp/MainAbility.java: -------------------------------------------------------------------------------- 1 | package com.zzrv5.zzrhttp; 2 | 3 | import com.zzrv5.zzrhttp.slice.MainAbilitySlice; 4 | import ohos.aafwk.ability.Ability; 5 | import ohos.aafwk.content.Intent; 6 | 7 | public class MainAbility extends Ability { 8 | @Override 9 | public void onStart(Intent intent) { 10 | super.onStart(intent); 11 | super.setMainRoute(MainAbilitySlice.class.getName()); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /mylibrary/src/main/java/com/zzrv5/mylibrary/ZZRResponse.java: -------------------------------------------------------------------------------- 1 | package com.zzrv5.mylibrary; 2 | 3 | import java.io.InputStream; 4 | 5 | /** 6 | * the HTTP response object 7 | * @author ZZR , Email:program.java@qq.com 8 | * @version 1.0.0 9 | * @since 2020-10-3 10 | */ 11 | public class ZZRResponse { 12 | public InputStream inputStream; 13 | public InputStream errorStream; 14 | public int code; 15 | public long contentLength; 16 | public Exception exception; 17 | } 18 | -------------------------------------------------------------------------------- /mylibrary/src/main/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "app": { 3 | "bundleName": "com.zzrv5.zzrhttp", 4 | "vendor": "zzrv5", 5 | "version": { 6 | "code": 1, 7 | "name": "1.0" 8 | }, 9 | "apiVersion": { 10 | "compatible": 3, 11 | "target": 3 12 | } 13 | }, 14 | "deviceConfig": {}, 15 | "module": { 16 | "package": "com.zzrv5.mylibrary", 17 | "deviceType": [ 18 | "tv" 19 | ], 20 | "distro": { 21 | "deliveryWithInstall": true, 22 | "moduleName": "mylibrary", 23 | "moduleType": "har" 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. DevEco Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | # If the Chinese output is garbled, please configure the following parameter. 10 | # org.gradle.jvmargs=-Dfile.encoding=GBK 11 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 14 | 15 | -------------------------------------------------------------------------------- /mylibrary/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.huawei.ohos.library' 2 | apply plugin: 'com.novoda.bintray-release' 3 | ohos { 4 | compileSdkVersion 3 5 | defaultConfig { 6 | compatibleSdkVersion 3 7 | } 8 | 9 | } 10 | 11 | dependencies { 12 | implementation fileTree(dir: 'libs', include: ['*.jar']) 13 | testCompile'junit:junit:4.12' 14 | } 15 | publish { 16 | userOrg = 'zzrv5' 17 | repoName = 'ZZRHttp' 18 | groupId = 'com.zzrv5.zzrhttp' 19 | artifactId = 'ZZRHttp' 20 | publishVersion = '1.0.1' 21 | desc = 'Oh hi, this is a nice description for a project, right?' 22 | website = 'https://github.com/zzrv5/ZZRHttp' 23 | } 24 | -------------------------------------------------------------------------------- /entry/src/main/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "app": { 3 | "bundleName": "com.zzrv5.zzrhttp", 4 | "vendor": "zzrv5", 5 | "version": { 6 | "code": 1, 7 | "name": "1.0" 8 | }, 9 | "apiVersion": { 10 | "compatible": 3, 11 | "target": 3 12 | } 13 | }, 14 | "deviceConfig": { 15 | "default": { 16 | "network": { 17 | "cleartextTraffic": false 18 | } 19 | } 20 | 21 | }, 22 | "module": { 23 | "reqPermissions": [{ 24 | "name":"ohos.permission.INTERNET" 25 | }], 26 | "package": "com.zzrv5.zzrhttp", 27 | "name": ".MyApplication", 28 | "reqCapabilities": [ 29 | "video_support" 30 | ], 31 | "deviceType": [ 32 | "tv" 33 | ], 34 | "distro": { 35 | "deliveryWithInstall": true, 36 | "moduleName": "entry", 37 | "moduleType": "entry" 38 | }, 39 | "abilities": [ 40 | { 41 | "skills": [ 42 | { 43 | "entities": [ 44 | "entity.system.home" 45 | ], 46 | "actions": [ 47 | "action.system.home" 48 | ] 49 | } 50 | ], 51 | "orientation": "landscape", 52 | "formEnabled": false, 53 | "name": "com.zzrv5.zzrhttp.MainAbility", 54 | "icon": "$media:icon", 55 | "description": "$string:mainability_description", 56 | "label": "ZZRHttp", 57 | "type": "page", 58 | "launchType": "standard" 59 | } 60 | ] 61 | } 62 | } -------------------------------------------------------------------------------- /mylibrary/src/main/java/com/zzrv5/mylibrary/ZZRHttp.java: -------------------------------------------------------------------------------- 1 | package com.zzrv5.mylibrary; 2 | 3 | import java.util.Map; 4 | 5 | /** 6 | * Encapsulates a thread's callback operation 7 | * @author ZZR ,program.java@qq.com 8 | * @version 1.0.0 9 | * @since 2020-10-3 10 | */ 11 | public class ZZRHttp { 12 | private static final String METHOD_GET = "GET"; 13 | private static final String METHOD_POST = "POST"; 14 | 15 | public static final String FILE_TYPE_FILE = "file/*"; 16 | public static final String FILE_TYPE_IMAGE = "image/*"; 17 | public static final String FILE_TYPE_AUDIO = "audio/*"; 18 | public static final String FILE_TYPE_VIDEO = "video/*"; 19 | 20 | 21 | public static void get(String url, ZZRCallBack callBack) { 22 | get(url, null, null, callBack); 23 | } 24 | 25 | 26 | public static void get(String url, Map paramsMap, ZZRCallBack callBack) { 27 | get(url, paramsMap, null, callBack); 28 | } 29 | 30 | 31 | public static void get(String url, Map paramsMap, Map headerMap, ZZRCallBack callBack) { 32 | new RequestUtil(METHOD_GET, url, paramsMap, headerMap, callBack).execute(); 33 | } 34 | 35 | 36 | public static void post(String url, ZZRCallBack callBack) { 37 | post(url, null, callBack); 38 | } 39 | 40 | 41 | public static void post(String url, Map paramsMap, ZZRCallBack callBack) { 42 | post(url, paramsMap, null, callBack); 43 | } 44 | 45 | 46 | public static void post(String url, Map paramsMap, Map headerMap, ZZRCallBack callBack) { 47 | new RequestUtil(METHOD_POST,url,paramsMap,headerMap,callBack).execute(); 48 | } 49 | 50 | public static void postJson(String url, String jsonStr, ZZRCallBack callBack) { 51 | postJson(url, jsonStr, null, callBack); 52 | } 53 | 54 | 55 | public static void postJson(String url, String jsonStr, Map headerMap, ZZRCallBack callBack) { 56 | new RequestUtil(url,jsonStr,headerMap,callBack).execute(); 57 | } 58 | 59 | 60 | 61 | } 62 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ZZRHttp 2 | 在鸿蒙(HarmonyOS)环境下,优雅的完成Http访问网络。 3 | ## 序 4 | 网络请求在现代的应用开发中必不可少,我们熟知的网络请求框架还真不少,像HttpClient、OKHttp还有volley等,它们确实方便,但鸿蒙还不能使用它们,还好我们有HttpURLConnection。原始的网络访问,再加上多线程,会使程序臃肿。 5 | ## 简介 6 | 我们希望的网络请求是这样的: 7 | 8 | * 隐藏网络访问细节; 9 | * 优雅处理UI更新。 10 | 11 | 基于以上两点我封装了此工具类,它有效地处理了Http访问,并融入了鸿蒙(HarmonyOS)的线程访问,可以方便我们在请求结束时更新UI。 12 | ## 使用步骤: 13 | ### 第一步,添加网络权限 14 | 在config.json文件中的module中添加,网络访问权限: 15 | ```javascript 16 | "module": { 17 | "reqPermissions": [{"name":"ohos.permission.INTERNET"}], 18 | ... 19 | ``` 20 | ### 第二步,确认网络模式 21 | 鸿蒙的默认是https访问模式,如果您的请求网址是http开头的,请在config.json文件中的deviceConfig下,添加如下设置: 22 | ```javascript 23 | "deviceConfig": { 24 | "default": { 25 | "network": { 26 | "cleartextTraffic": true 27 | } 28 | } 29 | }, 30 | ``` 31 | ### 第三步,添加依赖 32 | 在build.gradle文件的dependencies中,添加如下配置,引入ZZRhttp: 33 | ```javascript 34 | dependencies { 35 | implementation 'com.zzrv5.zzrhttp:ZZRHttp:1.0.1' 36 | ... 37 | } 38 | ``` 39 | ### 第四步,进行网络访问 40 | ```javascript 41 | ZZRHttp.get(url, new ZZRCallBack.CallBackString() { 42 | @Override 43 | public void onFailure(int code, String errorMessage) { 44 | //http访问出错了,此部分内容在主线程中工作; 45 | //可以更新UI等操作,请不要执行阻塞操作。 46 | } 47 | @Override 48 | public void onResponse(String response) { 49 | //http访问成功,此部分内容在主线程中工作; 50 | //可以更新UI等操作,但请不要执行阻塞操作。 51 | } 52 | }); 53 | ``` 54 | 55 | 56 | ## 常用方法说明 57 | 你即可以使用基本的简单请求,也可以使用带有请求参数和请求头的请求,方法如下: 58 | 59 | * get(String url, ZZRCallBack callBack) 60 | * get(String url, Map paramsMap, ZZRCallBack callBack) 61 | * get(String url, Map paramsMap, Map headerMap, ZZRCallBack callBack) 62 | * post(String url, ZZRCallBack callBack) 63 | * post(String url, Map paramsMap, ZZRCallBack callBack) 64 | * post(String url, Map paramsMap, Map headerMap, ZZRCallBack callBack) 65 | * postJson(String url, String jsonStr, ZZRCallBack callBack) 66 | * postJson(String url, String jsonStr, Map headerMap, ZZRCallBack callBack) 67 | 68 | 69 | ## 有问题反馈 70 | 在使用中有任何问题,欢迎反馈给我,可以用以下联系方式跟我交流 71 | 72 | * 邮件(program.java#qq.com, 把#换成@) 73 | * 微信:zzrizzr 74 | 75 | ## 关于作者 76 | 我是ZZR老师,为什么叫ZZR老师,是因为ZZR是我名字的缩写,在B站、51CTO、今日头条。搜索:ZZR老师 ,都可以找到我。 77 | ## 感激 78 | 感谢那些致力于鸿蒙生态的默默无闻的人们(排名不分先后,我是按着51CTO HarmonyOS社区的王雪燕女士的朋友圈出场顺序写的人名...): 79 | 80 | 高莹、王雪燕、朱友鹏、张荣超、张飞、谢昆明、小阳、韦东山、唐佐林、李宁、董昱老师等,感谢你们的兢兢业业、无私奉献出那么多优质的鸿蒙课程。 81 | 82 | 今天是2020年10月1日,双节将至,祝你们中秋快乐,祝愿祖国繁荣昌盛。 83 | 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /entry/src/main/java/com/zzrv5/zzrhttp/slice/MainAbilitySlice.java: -------------------------------------------------------------------------------- 1 | package com.zzrv5.zzrhttp.slice; 2 | 3 | import com.zzrv5.mylibrary.ZZRCallBack; 4 | import com.zzrv5.mylibrary.ZZRHttp; 5 | import ohos.aafwk.ability.AbilitySlice; 6 | import ohos.aafwk.content.Intent; 7 | 8 | import ohos.agp.components.Component; 9 | import ohos.agp.components.DirectionalLayout; 10 | import ohos.agp.components.DirectionalLayout.LayoutConfig; 11 | import ohos.agp.components.Text; 12 | import ohos.agp.colors.RgbColor; 13 | import ohos.agp.components.element.ShapeElement; 14 | import ohos.agp.utils.Color; 15 | import ohos.agp.utils.TextAlignment; 16 | 17 | public class MainAbilitySlice extends AbilitySlice { 18 | 19 | private DirectionalLayout myLayout = new DirectionalLayout(this); 20 | 21 | @Override 22 | public void onStart(Intent intent) { 23 | super.onStart(intent); 24 | LayoutConfig config = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT); 25 | myLayout.setLayoutConfig(config); 26 | ShapeElement element = new ShapeElement(); 27 | element.setRgbColor(new RgbColor(255, 255, 255)); 28 | myLayout.setBackground(element); 29 | 30 | Text text = new Text(this); 31 | text.setLayoutConfig(config); 32 | text.setText("Get 请求测试"); 33 | text.setTextColor(new Color(0xFF000000)); 34 | text.setTextSize(50); 35 | text.setTextAlignment(TextAlignment.CENTER); 36 | text.setClickedListener(new Component.ClickedListener() { 37 | @Override 38 | public void onClick(Component component) { 39 | text.setText("正在get访问,请稍后..."); 40 | String url="https://zzrv5.github.io/test/zzr.html"; 41 | ZZRHttp.get(url, new ZZRCallBack.CallBackString() { 42 | @Override 43 | public void onFailure(int code, String errorMessage) { 44 | //http访问出错了,注意此部分内容在主线程中工作,可以更新UI等操作,但请不要执行阻塞操作。 45 | text.setText("get访问出错:"+errorMessage); 46 | } 47 | @Override 48 | public void onResponse(String response) { 49 | //http访问成功,注意此部分内容在主线程中工作,可以更新UI等操作,但请不要执行阻塞操作。 50 | text.setText("服务器页面内容为(get):"+response); 51 | } 52 | }); 53 | } 54 | }); 55 | myLayout.addComponent(text); 56 | super.setUIContent(myLayout); 57 | } 58 | 59 | @Override 60 | public void onActive() { 61 | super.onActive(); 62 | } 63 | 64 | @Override 65 | public void onForeground(Intent intent) { 66 | super.onForeground(intent); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /mylibrary/src/main/java/com/zzrv5/mylibrary/ZZRCallBack.java: -------------------------------------------------------------------------------- 1 | package com.zzrv5.mylibrary; 2 | 3 | 4 | import ohos.eventhandler.EventHandler; 5 | import ohos.eventhandler.EventRunner; 6 | 7 | import java.io.BufferedReader; 8 | import java.io.InputStream; 9 | import java.io.InputStreamReader; 10 | 11 | /** 12 | * Encapsulates a thread's callback operation 13 | * @author ZZR ,program.java@qq.com 14 | * @version 1.0.0 15 | * @since 2020-10-3 16 | */ 17 | public abstract class ZZRCallBack { 18 | static EventHandler mMainHandler = new EventHandler(EventRunner.getMainEventRunner()); 19 | 20 | public void onProgress(float progress, long total ){} 21 | 22 | void onError(final ZZRResponse response){ 23 | 24 | final String errorMessage; 25 | if(response.inputStream != null){ 26 | errorMessage = getRetString(response.inputStream); 27 | }else if(response.errorStream != null) { 28 | errorMessage = getRetString(response.errorStream); 29 | }else if(response.exception != null) { 30 | errorMessage = response.exception.getMessage(); 31 | }else { 32 | errorMessage = ""; 33 | } 34 | mMainHandler.postTask(new Runnable() { 35 | @Override 36 | public void run() { 37 | onFailure(response.code,errorMessage); 38 | } 39 | }); 40 | } 41 | void onSeccess(ZZRResponse response){ 42 | final T obj = onParseResponse(response); 43 | mMainHandler.postTask(new Runnable() { 44 | @Override 45 | public void run() { 46 | onResponse(obj); 47 | } 48 | }); 49 | } 50 | 51 | 52 | public abstract T onParseResponse(ZZRResponse response); 53 | 54 | 55 | public abstract void onFailure(int code,String errorMessage); 56 | 57 | 58 | public abstract void onResponse(T response); 59 | 60 | 61 | 62 | public static abstract class CallBackDefault extends ZZRCallBack { 63 | @Override 64 | public ZZRResponse onParseResponse(ZZRResponse response) { 65 | return response; 66 | } 67 | } 68 | 69 | 70 | public static abstract class CallBackString extends ZZRCallBack { 71 | @Override 72 | public String onParseResponse(ZZRResponse response) { 73 | try { 74 | return getRetString(response.inputStream); 75 | } catch (Exception e) { 76 | throw new RuntimeException("failure"); 77 | } 78 | } 79 | } 80 | 81 | private static String getRetString(InputStream is) { 82 | String buf; 83 | try { 84 | BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8")); 85 | StringBuilder sb = new StringBuilder(); 86 | String line = ""; 87 | while ((line = reader.readLine()) != null) { 88 | sb.append(line + "\n"); 89 | } 90 | is.close(); 91 | buf = sb.toString(); 92 | return buf; 93 | 94 | } catch (Exception e) { 95 | return null; 96 | } 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /mylibrary/src/main/java/com/zzrv5/mylibrary/ZZRRequest.java: -------------------------------------------------------------------------------- 1 | package com.zzrv5.mylibrary; 2 | 3 | import java.io.BufferedWriter; 4 | import java.io.IOException; 5 | import java.io.OutputStreamWriter; 6 | import java.net.HttpURLConnection; 7 | import java.net.URL; 8 | import java.util.Map; 9 | 10 | /** 11 | * Thread's callback operation 12 | * @author ZZR ,program.java@qq.com 13 | * @version 1.0.0 14 | * @since 2020-10-3 15 | */ 16 | 17 | class ZZRRequest { 18 | 19 | 20 | /** 21 | * get 22 | */ 23 | ZZRResponse getData(String requestURL, Map headerMap){ 24 | HttpURLConnection conn = null; 25 | try { 26 | conn= getHttpURLConnection(requestURL,"GET"); 27 | conn.setDoInput(true); 28 | if(headerMap != null){ 29 | setHeader(conn,headerMap); 30 | } 31 | conn.connect(); 32 | return getRealResponse(conn); 33 | } catch (Exception e) { 34 | return getExceptonResponse(conn, e); 35 | } 36 | } 37 | 38 | /** 39 | * post 40 | */ 41 | ZZRResponse postData(String requestURL, String body, String bodyType, Map headerMap) { 42 | HttpURLConnection conn = null; 43 | try { 44 | conn = getHttpURLConnection(requestURL,"POST"); 45 | conn.setDoOutput(true); 46 | conn.setDoInput(true); 47 | conn.setUseCaches(false); 48 | if(!TextUtils.isEmpty(bodyType)) { 49 | conn.setRequestProperty("Content-Type", bodyType); 50 | } 51 | if(headerMap != null){ 52 | setHeader(conn,headerMap); 53 | } 54 | conn.connect(); 55 | if(!TextUtils.isEmpty(body)) { 56 | BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8")); 57 | writer.write(body); 58 | writer.close(); 59 | } 60 | return getRealResponse(conn); 61 | } catch (Exception e) { 62 | return getExceptonResponse(conn, e); 63 | } 64 | } 65 | 66 | 67 | private HttpURLConnection getHttpURLConnection(String requestURL,String requestMethod) throws IOException { 68 | URL url = new URL(requestURL); 69 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 70 | conn.setConnectTimeout(10*1000); 71 | conn.setReadTimeout(15*1000); 72 | conn.setRequestMethod(requestMethod); 73 | return conn; 74 | } 75 | 76 | 77 | private void setHeader(HttpURLConnection conn, Map headerMap) { 78 | if(headerMap != null){ 79 | for (String key: headerMap.keySet()){ 80 | conn.setRequestProperty(key, headerMap.get(key)); 81 | } 82 | } 83 | } 84 | 85 | 86 | private ZZRResponse getRealResponse(HttpURLConnection conn) throws IOException { 87 | ZZRResponse response = new ZZRResponse(); 88 | response.code = conn.getResponseCode(); 89 | response.contentLength = conn.getContentLength(); 90 | response.inputStream = conn.getInputStream(); 91 | response.errorStream = conn.getErrorStream(); 92 | return response; 93 | } 94 | 95 | 96 | private ZZRResponse getExceptonResponse(HttpURLConnection conn, Exception e) { 97 | if(conn != null){ 98 | conn.disconnect(); 99 | } 100 | e.printStackTrace(); 101 | ZZRResponse response = new ZZRResponse(); 102 | response.exception = e; 103 | return response; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /mylibrary/src/main/java/com/zzrv5/mylibrary/RequestUtil.java: -------------------------------------------------------------------------------- 1 | package com.zzrv5.mylibrary; 2 | 3 | import java.io.UnsupportedEncodingException; 4 | import java.net.HttpURLConnection; 5 | import java.net.URLEncoder; 6 | import java.util.Map; 7 | 8 | /** 9 | * Http Request Util 10 | * @author ZZR ,program.java@qq.com 11 | * @version 1.0.0 12 | * @since 2020-10-3 13 | */ 14 | class RequestUtil { 15 | private Thread mThread; 16 | /** 17 | * General get or POST 18 | */ 19 | RequestUtil(String method, String url, Map paramsMap, Map headerMap, ZZRCallBack callBack) { 20 | switch (method){ 21 | case "GET": 22 | urlHttpGet(url,paramsMap,headerMap,callBack); 23 | break; 24 | case "POST": 25 | urlHttpPost(url,paramsMap,null,headerMap,callBack); 26 | break; 27 | } 28 | } 29 | 30 | /** 31 | * Post requests for JSON 32 | */ 33 | RequestUtil(String url, String jsonStr, Map headerMap, ZZRCallBack callBack) { 34 | urlHttpPost(url,null,jsonStr,headerMap,callBack); 35 | } 36 | 37 | 38 | 39 | private void urlHttpGet(final String url, final Map paramsMap, final Map headerMap, final ZZRCallBack callBack) { 40 | mThread = new Thread(new Runnable() { 41 | @Override 42 | public void run() { 43 | ZZRResponse response = new ZZRRequest().getData(getUrl(url,paramsMap),headerMap); 44 | if(response.code == HttpURLConnection.HTTP_OK){ 45 | callBack.onSeccess(response); 46 | }else { 47 | callBack.onError(response); 48 | } 49 | } 50 | 51 | }); 52 | } 53 | 54 | 55 | private void urlHttpPost(final String url, final Map paramsMap, final String jsonStr, final Map headerMap, final ZZRCallBack callBack) { 56 | mThread = new Thread(new Runnable() { 57 | @Override 58 | public void run() { 59 | ZZRResponse response = new ZZRRequest().postData(url, getPostBody(paramsMap,jsonStr),getPostBodyType(paramsMap,jsonStr),headerMap); 60 | if(response.code == HttpURLConnection.HTTP_OK){ 61 | callBack.onSeccess(response); 62 | }else { 63 | callBack.onError(response); 64 | } 65 | 66 | } 67 | 68 | }); 69 | 70 | } 71 | 72 | 73 | 74 | 75 | 76 | 77 | private String getUrl(String path,Map paramsMap) { 78 | if(paramsMap != null){ 79 | path = path+"?"; 80 | for (String key: paramsMap.keySet()){ 81 | path = path + key+"="+paramsMap.get(key)+"&"; 82 | } 83 | path = path.substring(0,path.length()-1); 84 | } 85 | return path; 86 | } 87 | 88 | 89 | private String getPostBody(Map params,String jsonStr) {//throws UnsupportedEncodingException { 90 | if(params != null){ 91 | return getPostBodyFormParameMap(params); 92 | }else if(!TextUtils.isEmpty(jsonStr)){ 93 | return jsonStr; 94 | } 95 | return null; 96 | } 97 | 98 | 99 | 100 | private String getPostBodyFormParameMap(Map params) {//throws UnsupportedEncodingException { 101 | StringBuilder result = new StringBuilder(); 102 | boolean first = true; 103 | try { 104 | for (Map.Entry entry : params.entrySet()) { 105 | if (first) 106 | first = false; 107 | else 108 | result.append("&"); 109 | result.append(URLEncoder.encode(entry.getKey(), "UTF-8")); 110 | result.append("="); 111 | result.append(URLEncoder.encode(entry.getValue(), "UTF-8")); 112 | } 113 | return result.toString(); 114 | } catch (UnsupportedEncodingException e) { 115 | return null; 116 | } 117 | 118 | } 119 | 120 | 121 | private String getPostBodyType(Map paramsMap, String jsonStr) { 122 | if(paramsMap != null){ 123 | //return "text/plain"; unknown error 124 | 125 | return null; 126 | }else if(!TextUtils.isEmpty(jsonStr)){ 127 | return "application/json;charset=utf-8"; 128 | } 129 | return null; 130 | } 131 | /** 132 | * Execute thread 133 | */ 134 | void execute(){ 135 | if(mThread != null){ 136 | mThread.start(); 137 | } 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | --------------------------------------------------------------------------------