|
| 1 | +# Android 使用 tio-boot |
| 2 | + |
| 3 | +build.gradle 添加依赖 |
| 4 | + |
| 5 | +```xml |
| 6 | + //litongjava |
| 7 | + implementation 'com.litongjava:android-view-inject:1.0' |
| 8 | + implementation 'com.litongjava:litongjava-android-utils:1.0.0' |
| 9 | + implementation 'com.litongjava:tio-boot:1.3.7' |
| 10 | +``` |
| 11 | + |
| 12 | +controller |
| 13 | + |
| 14 | +```java |
| 15 | +package com.litongjava.android.tio.boot.controller; |
| 16 | + |
| 17 | +import com.litongjava.tio.http.common.HttpRequest; |
| 18 | +import com.litongjava.tio.http.common.HttpResponse; |
| 19 | +import com.litongjava.tio.http.server.util.Resps; |
| 20 | + |
| 21 | +public class HelloController { |
| 22 | + |
| 23 | + public HttpResponse hello(HttpRequest httpRequest) { |
| 24 | + return Resps.txt(httpRequest, "hello"); |
| 25 | + } |
| 26 | + |
| 27 | + public HttpResponse hi(HttpRequest httpRequest) { |
| 28 | + return Resps.txt(httpRequest, "hi"); |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +使用 TioBootServerApp 启动服务 |
| 34 | + |
| 35 | +```java |
| 36 | +package com.litongjava.android.tio.boot.controller; |
| 37 | + |
| 38 | +import com.blankj.utilcode.util.NetworkUtils; |
| 39 | +import com.litongjava.android.tio.boot.MainActivity; |
| 40 | +import com.litongjava.jfinal.aop.Aop; |
| 41 | +import com.litongjava.jfinal.aop.annotation.AImport; |
| 42 | +import com.litongjava.tio.boot.TioApplication; |
| 43 | +import com.litongjava.tio.boot.server.TioBootServer; |
| 44 | +import com.litongjava.tio.http.server.handler.SimpleHttpRoutes; |
| 45 | + |
| 46 | +import org.slf4j.Logger; |
| 47 | +import org.slf4j.LoggerFactory; |
| 48 | + |
| 49 | +//不支持 |
| 50 | +//@AImport(IndexController.class) |
| 51 | +public class TioBootServerApp { |
| 52 | + private static Logger log = LoggerFactory.getLogger(TioBootServerApp.class); |
| 53 | + |
| 54 | + public static void run() { |
| 55 | + long start = System.currentTimeMillis(); |
| 56 | + // 创建simpleHttpRoutes |
| 57 | + SimpleHttpRoutes simpleHttpRoutes = new SimpleHttpRoutes(); |
| 58 | + // 创建controller |
| 59 | + HelloController helloController = Aop.get(HelloController.class); |
| 60 | + |
| 61 | + // 添加action |
| 62 | + simpleHttpRoutes.add("/hi", helloController::hi); |
| 63 | + simpleHttpRoutes.add("/hello", helloController::hello); |
| 64 | + |
| 65 | + // 将simpleHttpRoutes添加到TioBootServer |
| 66 | + TioBootServer.setHttpRoutes(simpleHttpRoutes); |
| 67 | + |
| 68 | + String ipAddressByWifi = NetworkUtils.getIpAddressByWifi(); |
| 69 | + log.info("ipAddressByWifi:{}", ipAddressByWifi); |
| 70 | + String[] args = new String[]{"--server.port=10051", "--tio.mvc.route.printMapping=true"}; |
| 71 | + TioApplication.run(TioBootServerApp.class, args); |
| 72 | + long end = System.currentTimeMillis(); |
| 73 | + System.out.println((end - start) + "(ms)"); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +``` |
| 78 | + |
| 79 | +AndroidManifest.xml 添加权限 |
| 80 | + |
| 81 | +```xml |
| 82 | +<?xml version="1.0" encoding="utf-8"?> |
| 83 | +<manifest xmlns:android="http://schemas.android.com/apk/res/android" |
| 84 | + package="com.litongjava.android.tio.boot"> |
| 85 | + |
| 86 | + <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> |
| 87 | + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> |
| 88 | + |
| 89 | + <uses-permission android:name="android.permission.INTERNET" /> |
| 90 | + <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> |
| 91 | + |
| 92 | + <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> |
| 93 | + |
| 94 | + |
| 95 | + <application |
| 96 | + android:allowBackup="true" |
| 97 | + android:icon="@mipmap/ic_launcher" |
| 98 | + android:label="@string/app_name" |
| 99 | + android:roundIcon="@mipmap/ic_launcher_round" |
| 100 | + android:supportsRtl="true" |
| 101 | + android:theme="@style/Theme.Litongjavatiobootforandroidstudy"> |
| 102 | + <activity android:name=".MainActivity"> |
| 103 | + <intent-filter> |
| 104 | + <action android:name="android.intent.action.MAIN" /> |
| 105 | + |
| 106 | + <category android:name="android.intent.category.LAUNCHER" /> |
| 107 | + </intent-filter> |
| 108 | + </activity> |
| 109 | + </application> |
| 110 | + |
| 111 | +</manifest> |
| 112 | +``` |
| 113 | + |
| 114 | +activity_main.xml 添加一个按钮 |
| 115 | + |
| 116 | +```xml |
| 117 | +<?xml version="1.0" encoding="utf-8"?> |
| 118 | +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
| 119 | + xmlns:tools="http://schemas.android.com/tools" |
| 120 | + android:layout_width="match_parent" |
| 121 | + android:layout_height="match_parent" |
| 122 | + tools:context=".MainActivity"> |
| 123 | + |
| 124 | + <Button |
| 125 | + android:id="@+id/startBtn" |
| 126 | + android:layout_width="wrap_content" |
| 127 | + android:layout_height="wrap_content" |
| 128 | + android:background="@color/purple_200" |
| 129 | + android:text="启动"></Button> |
| 130 | + |
| 131 | +</LinearLayout> |
| 132 | +``` |
| 133 | + |
| 134 | +MainActivity 获取权限 启动服务 |
| 135 | + |
| 136 | +```java |
| 137 | +package com.litongjava.android.tio.boot; |
| 138 | + |
| 139 | +import androidx.appcompat.app.AppCompatActivity; |
| 140 | + |
| 141 | +import android.Manifest; |
| 142 | +import android.os.Bundle; |
| 143 | +import android.view.View; |
| 144 | + |
| 145 | +import com.blankj.utilcode.util.NetworkUtils; |
| 146 | +import com.litongjava.android.tio.boot.controller.HelloController; |
| 147 | +import com.litongjava.android.tio.boot.controller.IndexController; |
| 148 | +import com.litongjava.android.tio.boot.controller.TioBootServerApp; |
| 149 | +import com.litongjava.android.utils.acp.AcpUtils; |
| 150 | +import com.litongjava.android.utils.toast.ToastUtils; |
| 151 | +import com.litongjava.android.view.inject.annotation.FindViewByIdLayout; |
| 152 | +import com.litongjava.android.view.inject.annotation.OnClick; |
| 153 | +import com.litongjava.android.view.inject.utils.ViewInjectUtils; |
| 154 | +import com.litongjava.jfinal.aop.Aop; |
| 155 | +import com.litongjava.tio.boot.TioApplication; |
| 156 | +import com.litongjava.tio.boot.http.routes.TioBootHttpRoutes; |
| 157 | +import com.litongjava.tio.boot.server.TioBootServer; |
| 158 | +import com.litongjava.tio.http.server.handler.HttpRoutes; |
| 159 | +import com.litongjava.tio.http.server.handler.SimpleHttpRoutes; |
| 160 | +import com.mylhyl.acp.AcpListener; |
| 161 | + |
| 162 | +import org.slf4j.Logger; |
| 163 | +import org.slf4j.LoggerFactory; |
| 164 | + |
| 165 | +import java.util.Arrays; |
| 166 | +import java.util.List; |
| 167 | + |
| 168 | +@FindViewByIdLayout(R.layout.activity_main) |
| 169 | +public class MainActivity extends AppCompatActivity { |
| 170 | + private Logger log = LoggerFactory.getLogger(this.getClass()); |
| 171 | + |
| 172 | + @Override |
| 173 | + protected void onCreate(Bundle savedInstanceState) { |
| 174 | + super.onCreate(savedInstanceState); |
| 175 | + //setContentView(R.layout.activity_main); |
| 176 | + ViewInjectUtils.injectActivity(this, this); |
| 177 | + } |
| 178 | + |
| 179 | + @OnClick(R.id.startBtn) |
| 180 | + public void startBtnOnClick(View view) { |
| 181 | + String[] permissions = { |
| 182 | + //写入外部设备权限 |
| 183 | + Manifest.permission.ACCESS_NETWORK_STATE, |
| 184 | + Manifest.permission.ACCESS_WIFI_STATE, |
| 185 | + Manifest.permission.INTERNET, |
| 186 | + Manifest.permission.READ_EXTERNAL_STORAGE, |
| 187 | + Manifest.permission.WRITE_EXTERNAL_STORAGE |
| 188 | + |
| 189 | + }; |
| 190 | + //创建acpListener |
| 191 | + AcpListener acpListener = new AcpListener() { |
| 192 | + @Override |
| 193 | + public void onGranted() { |
| 194 | + TioBootServerApp.run(); |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public void onDenied(List<String> permissions) { |
| 199 | + ToastUtils.defaultToast(getApplicationContext(), permissions.toString() + "权限拒绝,无法写入日志"); |
| 200 | + } |
| 201 | + }; |
| 202 | + |
| 203 | + AcpUtils.requestPermissions(this, permissions, acpListener); |
| 204 | + |
| 205 | + } |
| 206 | +} |
| 207 | +``` |
| 208 | + |
| 209 | +启动日志 |
| 210 | + |
| 211 | +```xml |
| 212 | +2024-01-28 22:58:28.280 [main] INFO TioBootServerApp.run:34 - ipAddressByWifi:10.0.1.64 |
| 213 | +2024-01-28 22:58:28.347 [main] INFO TioServer.start:167 - |
| 214 | +|----------------------------------------------------------------------------------------| |
| 215 | +| t-io site | https://www.litongjava.com/t-io | |
| 216 | +| t-io on gitee | https://gitee.com/ppnt/t-io | |
| 217 | +| t-io on github | https://github.com/litongjava/t-io | |
| 218 | +| t-io version | 3.7.3.v20240113-RELEASE | |
| 219 | +| ---------------------------------------------------------------------------------------| |
| 220 | +| TioConfig name | tio-boot | |
| 221 | +| Started at | 2024-01-28 22:58:28 | |
| 222 | +| Listen on | 0.0.0.0:10051 | |
| 223 | +| Main Class | com.android.internal.os.ZygoteInit | |
| 224 | +| Jvm start time | Not available in Android | |
| 225 | +| Tio start time | 37ms | |
| 226 | +| Pid | Not available in Android | |
| 227 | +|----------------------------------------------------------------------------------------| |
| 228 | +2024-01-28 22:58:28.351 [main] INFO TioApplicationContext.run:128 - scan class and init:8(ms),server:58(ms),config:1(ms),http reoute:0(ms) |
| 229 | +2024-01-28 22:58:28.353 [main] INFO TioApplicationContext.printUrl:146 - port:10051 |
| 230 | +http://localhost:10051 |
| 231 | +108(ms) |
| 232 | +``` |
| 233 | + |
| 234 | +1.注意 tio-boot 的 controller 不支持在 android 系统中运行 |
| 235 | +2.tio-http-server 的 action 支持在 Android 系统中运行 |
0 commit comments