1
- # 在 Android 上使用 tio-boot 运行 HTTP 服务
1
+ # tio-boot 添加 http-server handler
2
2
3
- 本文档提供了在 Android 设备上通过 ` tio-boot ` 运行 HTTP 服务的详细步骤。 ` tio-boot ` 是一个轻量级的 HTTP 服务框架,可用于处理 HTTP 请求。
3
+ tio-boot 支持的两种组件最 http 请求进行处理
4
4
5
- ## 步骤 1: 添加依赖项
5
+ - http-server handler
6
+ - tio-boot controller
6
7
7
- 在你的` build.gradle ` 文件中添加以下依赖项,以引入必要的库。
8
+ 1.http-server handler 的优先级高于 tio-boot controller
9
+ 2.http-server handler 不支持参数封装,只能接受形参 HttpRequest,返回 HttpResponse
10
+ 3.http-server handler 在性能上优于 tio-boot controller
8
11
9
- ``` xml
10
- // 在build.gradle中添加以下依赖项以支持tio-boot和相关功能
11
- implementation 'com.litongjava:android-view-inject:1.0'
12
- implementation 'com.litongjava:litongjava-android-utils:1.0.0'
13
- implementation 'com.litongjava:tio-boot:1.3.7'
14
- ```
15
-
16
- ## 步骤 2: 创建 Controller
12
+ #### 步骤 1:创建 Controller 类
17
13
18
- 创建一个 Controller 类(例如 ` HelloController ` ),该类包含处理 HTTP 请求的方法。
14
+ 首先,创建一个名为 ` HelloController ` 的类,它包含了两个处理 HTTP 请求的方法。每个方法接收一个 ` HttpRequest ` 对象,并返回一个 ` HttpResponse ` 对象。例如:
19
15
20
16
``` java
21
- // 在com.litongjava.android.tio.boot.controller包下创建HelloController类
22
- package com.litongjava.android.tio.boot.controller ;
23
-
24
17
import com.litongjava.tio.http.common.HttpRequest ;
25
18
import com.litongjava.tio.http.common.HttpResponse ;
26
19
import com.litongjava.tio.http.server.util.Resps ;
@@ -37,31 +30,26 @@ public class HelloController {
37
30
}
38
31
```
39
32
40
- ## 步骤 3: 启动服务
33
+ 在这个例子中, ` hello ` 方法返回文本 "hello",而 ` hi ` 方法返回文本 "hi"。
41
34
42
- 使用 ` TioBootServerApp ` 类来配置和启动 HTTP 服务。
35
+ #### 步骤 2:定义并配置 HttpRoutes
43
36
44
- ``` java
45
- package com.litongjava.android.tio.boot.controller ;
37
+ 接下来,定义一个配置类 ` DefineHttpRoutesConfig ` 。在这个类中,你将实例化 ` SimpleHttpRoutes ` 并添加自定义路由。
46
38
47
- import com.blankj.utilcode.util.NetworkUtils ;
48
- import com.litongjava.android.tio.boot.MainActivity ;
39
+ 使用 ` @BeforeStartConfiguration ` 注解标记这个配置类,这样框架会在启动前加载它。通过 ` @ABean ` 注解定义一个 ` HttpRoutes ` 类型的方法 ` httpRoutes ` 。在这个方法中,首先通过 ` Aop.get ` 方法获取 ` HelloController ` 的实例。然后,创建一个 ` SimpleHttpRoutes ` 实例,并使用 ` add ` 方法添加路由。
40
+
41
+ ``` java
49
42
import com.litongjava.jfinal.aop.Aop ;
50
- import com.litongjava.jfinal.aop.annotation.AImport ;
51
- import com.litongjava.tio.boot.TioApplication ;
43
+ import com.litongjava.jfinal.aop.annotation.AInitialization ;
44
+ import com.litongjava.jfinal.aop.annotation.BeforeStartConfiguration ;
52
45
import com.litongjava.tio.boot.server.TioBootServer ;
53
46
import com.litongjava.tio.http.server.handler.SimpleHttpRoutes ;
54
47
55
- import org.slf4j.Logger ;
56
- import org.slf4j.LoggerFactory ;
57
-
58
- // 不支持
59
- // @AImport(IndexController.class)
60
- public class TioBootServerApp {
61
- private static Logger log = LoggerFactory . getLogger(TioBootServerApp . class);
48
+ @BeforeStartConfiguration
49
+ public class DefineHttpRoutesConfig {
62
50
63
- public static void run () {
64
- long start = System . currentTimeMillis();
51
+ @AInitialization
52
+ public void httpRoutes () {
65
53
// 创建simpleHttpRoutes
66
54
SimpleHttpRoutes simpleHttpRoutes = new SimpleHttpRoutes ();
67
55
// 创建controller
@@ -73,187 +61,9 @@ public class TioBootServerApp {
73
61
74
62
// 将simpleHttpRoutes添加到TioBootServer
75
63
TioBootServer . setHttpRoutes(simpleHttpRoutes);
76
-
77
- String ipAddressByWifi = NetworkUtils . getIpAddressByWifi();
78
- log. info(" ipAddressByWifi:{}" , ipAddressByWifi);
79
- String [] args = new String []{" --server.port=10051" , " --tio.mvc.route.printMapping=true" };
80
- TioApplication . run(TioBootServerApp . class, args);
81
- long end = System . currentTimeMillis();
82
- System . out. println((end - start) + " (ms)" );
83
- }
84
- }
85
- ```
86
-
87
- 在这个类中,我们创建了一个` SimpleHttpRoutes ` 实例来定义路由,并将其添加到` TioBootServer ` 中。我们还通过 WiFi 获取 IP 地址,并启动了 TioApplication。
88
-
89
- ## 步骤 4: 配置 AndroidManifest.xml
90
-
91
- 在你的` AndroidManifest.xml ` 文件中添加必要的权限。
92
-
93
- ``` xml
94
- <?xml version =" 1.0" encoding =" utf-8" ?>
95
- <manifest xmlns : android =" http://schemas.android.com/apk/res/android"
96
- package =" com.litongjava.android.tio.boot" >
97
-
98
- <uses-permission android : name =" android.permission.READ_EXTERNAL_STORAGE" />
99
- <uses-permission android : name =" android.permission.WRITE_EXTERNAL_STORAGE" />
100
-
101
- <uses-permission android : name =" android.permission.INTERNET" />
102
- <uses-permission android : name =" android.permission.ACCESS_NETWORK_STATE" />
103
-
104
- <uses-permission android : name =" android.permission.ACCESS_WIFI_STATE" />
105
-
106
-
107
- <application
108
- android : allowBackup =" true"
109
- android : icon =" @mipmap/ic_launcher"
110
- android : label =" @string/app_name"
111
- android : roundIcon =" @mipmap/ic_launcher_round"
112
- android : supportsRtl =" true"
113
- android : theme =" @style/Theme.Litongjavatiobootforandroidstudy" >
114
- <activity android : name =" .MainActivity" >
115
- <intent-filter >
116
- <action android : name =" android.intent.action.MAIN" />
117
-
118
- <category android : name =" android.intent.category.LAUNCHER" />
119
- </intent-filter >
120
- </activity >
121
- </application >
122
-
123
- </manifest >
124
- ```
125
-
126
- ## 步骤 5: 设计 UI 界面
127
-
128
- 在` activity_main.xml ` 中添加一个按钮,用户可以通过点击该按钮来启动 HTTP 服务。
129
-
130
- ``` xml
131
- <?xml version =" 1.0" encoding =" utf-8" ?>
132
- <LinearLayout xmlns : android =" http://schemas.android.com/apk/res/android"
133
- xmlns : tools =" http://schemas.android.com/tools"
134
- android : layout_width =" match_parent"
135
- android : layout_height =" match_parent"
136
- tools : context =" .MainActivity" >
137
-
138
- <Button
139
- android : id =" @+id/startBtn"
140
- android : layout_width =" wrap_content"
141
- android : layout_height =" wrap_content"
142
- android : background =" @color/purple_200"
143
- android : text =" 启动" ></Button >
144
-
145
- </LinearLayout >
146
- ```
147
-
148
- ## 步骤 6: MainActivity 获取权限并启动服务
149
-
150
- 在` MainActivity ` 类中,请求必要的权限并在授权后启动服务。
151
-
152
- ``` java
153
- package com.litongjava.android.tio.boot ;
154
-
155
- import androidx.appcompat.app.AppCompatActivity ;
156
-
157
- import android.Manifest ;
158
- import android.os.Bundle ;
159
- import android.view.View ;
160
-
161
- import com.blankj.utilcode.util.NetworkUtils ;
162
- import com.litongjava.android.tio.boot.controller.HelloController ;
163
- import com.litongjava.android.tio.boot.controller.IndexController ;
164
- import com.litongjava.android.tio.boot.controller.TioBootServerApp ;
165
- import com.litongjava.android.utils.acp.AcpUtils ;
166
- import com.litongjava.android.utils.toast.ToastUtils ;
167
- import com.litongjava.android.view.inject.annotation.FindViewByIdLayout ;
168
- import com.litongjava.android.view.inject.annotation.OnClick ;
169
- import com.litongjava.android.view.inject.utils.ViewInjectUtils ;
170
- import com.litongjava.jfinal.aop.Aop ;
171
- import com.litongjava.tio.boot.TioApplication ;
172
- import com.litongjava.tio.boot.http.routes.TioBootHttpRoutes ;
173
- import com.litongjava.tio.boot.server.TioBootServer ;
174
- import com.litongjava.tio.http.server.handler.HttpRoutes ;
175
- import com.litongjava.tio.http.server.handler.SimpleHttpRoutes ;
176
- import com.mylhyl.acp.AcpListener ;
177
-
178
- import org.slf4j.Logger ;
179
- import org.slf4j.LoggerFactory ;
180
-
181
- import java.util.Arrays ;
182
- import java.util.List ;
183
-
184
- @FindViewByIdLayout (R . layout. activity_main)
185
- public class MainActivity extends AppCompatActivity {
186
- private Logger log = LoggerFactory . getLogger(this . getClass());
187
-
188
- @Override
189
- protected void onCreate (Bundle savedInstanceState ) {
190
- super . onCreate(savedInstanceState);
191
- // setContentView(R.layout.activity_main);
192
- ViewInjectUtils . injectActivity(this , this );
193
- }
194
-
195
- @OnClick (R . id. startBtn)
196
- public void startBtnOnClick (View view ) {
197
- String [] permissions = {
198
- // 写入外部设备权限
199
- Manifest . permission. ACCESS_NETWORK_STATE ,
200
- Manifest . permission. ACCESS_WIFI_STATE ,
201
- Manifest . permission. INTERNET ,
202
- Manifest . permission. READ_EXTERNAL_STORAGE ,
203
- Manifest . permission. WRITE_EXTERNAL_STORAGE
204
-
205
- };
206
- // 创建acpListener
207
- AcpListener acpListener = new AcpListener () {
208
- @Override
209
- public void onGranted () {
210
- TioBootServerApp . run();
211
- }
212
-
213
- @Override
214
- public void onDenied (List<String > permissions ) {
215
- ToastUtils . defaultToast(getApplicationContext(), permissions. toString() + " 权限拒绝,无法写入日志" );
216
- }
217
- };
218
-
219
- AcpUtils . requestPermissions(this , permissions, acpListener);
220
-
221
64
}
222
65
}
223
- ```
224
-
225
- 在这个类中,我们使用` AcpUtils ` 来请求权限,并在权限被授予后启动` TioBootServerApp ` 。
226
- 启动日志
227
66
228
- ``` xml
229
- 2024-01-28 22:58:28.280 [main] INFO TioBootServerApp.run:34 - ipAddressByWifi:10.0.1.64
230
- 2024-01-28 22:58:28.347 [main] INFO TioServer.start:167 -
231
- |----------------------------------------------------------------------------------------|
232
- | t-io site | https://www.litongjava.com/t-io |
233
- | t-io on gitee | https://gitee.com/ppnt/t-io |
234
- | t-io on github | https://github.com/litongjava/t-io |
235
- | t-io version | 3.7.3.v20240113-RELEASE |
236
- | ---------------------------------------------------------------------------------------|
237
- | TioConfig name | tio-boot |
238
- | Started at | 2024-01-28 22:58:28 |
239
- | Listen on | 0.0.0.0:10051 |
240
- | Main Class | com.android.internal.os.ZygoteInit |
241
- | Jvm start time | Not available in Android |
242
- | Tio start time | 37ms |
243
- | Pid | Not available in Android |
244
- |----------------------------------------------------------------------------------------|
245
- 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)
246
- 2024-01-28 22:58:28.353 [main] INFO TioApplicationContext.printUrl:146 - port:10051
247
- http://localhost:10051
248
- 108(ms)
249
67
```
250
68
251
- ## 测试访问
252
-
253
- - http://10.0.1.64:10051/hi
254
- - http://10.0.1.64:10051/hello
255
-
256
- ## 注意事项
257
-
258
- 1 . ` tio-boot ` 的` controller ` 不支持在 Android 系统中运行。
259
- 2 . ` tio-http-server ` 的` handler ` 支持在 Android 系统中运行。
69
+ 在上述配置中,` /hi ` 路由映射到 ` HelloController ` 的 ` hi ` 方法,而 ` /hello ` 路由映射到 ` hello ` 方法。
0 commit comments