# NetUtils **Repository Path**: y141111/NetUtils ## Basic Information - **Project Name**: NetUtils - **Description**: 网络工具,实现了TCPClientThread类 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-06-19 - **Last Updated**: 2025-06-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # NetUtils #### 介绍 网络工具 TCPClient 线程(封装好的带回调函数) HTTP 请求工具 #### 软件架构 软件架构说明 #### 安装教程 1. xxxx 2. xxxx 3. xxxx #### 使用说明 ##### 1. Gradle 中 引用本项目 1.添加 jitpack 仓库 ```gradle dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { mavenCentral() maven { url 'https://jitpack.io' } } } ``` 2.添加本项目 ```gradle dependencies { implementation 'com.gitee.y141111:NetUtils:1.1.1' } ``` ##### 2. TCPClientThread 用法 AndroidManifest.xml 中添加 网络权限 ```xml ``` 用法: ```java …… import net.moao.netutils_library.tcp.TCPClientThread; …… public static TCPClientThread tcpClient; …… …… // 创建 TCPClientThread 的回调函数 TCPClientThreadCallback callback = new TCPClientThreadCallback() { @Override public void onConn(boolean isConn) { // 线程连接回调,可为空 isConn 标志连接成功还是失败 showMsg("TCPClient 线程连接成功?" + isConn); } @Override public void onHeartbeat(TCPClient tcpClient) { // 接收超时时候的操作,一般用作心跳包 tcpClient.send("im alive..."); } @Override public void onReceive(byte[] msgBytes) { // 读取到数据 的回调方法 showMsg("RX:" + new String(msgBytes)); } @Override public void onRunLast(boolean isConnTCP) { // 线程准备关闭时执行的函数 showMsg("TCPClient 线程结束."); // 这里 由于 线程关闭后 无法重新 start() ,重新 赋值,回调函数调用自己 tcpClient = new TCPClientThread(false, ServerInfo.serverInfo.getServerDomain(), Integer.parseInt(ServerInfo.serverInfo.getServerPort()), null, this); } }; tcpClient = new TCPClientThread(false, ServerInfo.serverInfo.getServerDomain(), Integer.parseInt(ServerInfo.serverInfo.getServerPort()), null, callback); tcpClient.start(); …… ``` ##### 3. HTTPRequestGet 用法 如果需要用到 http 协议需要 设置 允许 http 请求 允许应用程序使用Http明文网络传输 参见:[Android | 允许 http 请求 允许应用程序使用Http明文网络传输](https://blog.1okk.com/archives/159.html) ```java new Thread(()->{ try { String response = String.valueOf(HttpRequest.sendGetRequest("http://ip.3322.net")); JSONArray jsonArray = JSON.parseArray(response); runOnUiThread(()->{ Toast.makeText(this, jsonArray.toJSONString(), Toast.LENGTH_SHORT).show(); }); } catch (Exception e) { Log.e(TAG, "onClick: ", e); runOnUiThread(()->{ Toast.makeText(this, "下载失败", Toast.LENGTH_SHORT).show(); }); } }).start(); ``` #### 更新日志 (更新日志)[./CHANGE_LOG.md] #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)