Skip to content

Commit 4b32396

Browse files
committed
Android:APIJSONTest 收敛可访问的 URL,退出界面自动停止服务
1 parent 60625d0 commit 4b32396

1 file changed

Lines changed: 10 additions & 14 deletions

File tree

‎APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/ui/UnitActivity.java‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,23 +132,13 @@ public void stop(View v) {
132132

133133

134134
private void startServer(int port) {
135-
// server.addAction("OPTIONS","*", this);
136-
// server.get("/test", new HttpServerRequestCallback() {
137-
// @Override
138-
// public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
139-
// response.send("{\"hello\": \"world!\"}");
140-
// }
141-
// });
142-
143-
// server.post("/get", this);
144135
server.addAction("OPTIONS", "[\\d\\D]*", this);
145-
server.get("[\\d\\D]*", this);
146-
// server.post("/get", this);
136+
// server.get("[\\d\\D]*", this);
147137
// server.post("[\\d\\D]*", this);
138+
server.get("/", this);
148139
server.post("/method/list", this);
149140
server.post("/method/invoke", this);
150141
server.listen(mAsyncServer, port);
151-
152142
}
153143

154144
@Override
@@ -181,7 +171,7 @@ public void onRequest(final AsyncHttpServerRequest asyncHttpServerRequest, final
181171

182172
@Override
183173
public void run() {
184-
if (isAlive) {
174+
if (isAlive) { //TODO 改为 ListView 展示,保证每次请求都能对齐 Request 和 Response 的显示
185175
tvUnitRequest.setText(StringUtil.getString(asyncHttpServerRequest) + "Content:\n" + zuo.biao.apijson.JSON.format(request) + "\n\n\n\n\n" + StringUtil.getString(tvUnitRequest));
186176
}
187177
}
@@ -195,6 +185,9 @@ public void run() {
195185
}
196186

197187
switch (asyncHttpServerRequest.getPath()) {
188+
case "/":
189+
send(asyncHttpServerResponse, "ok");
190+
break;
198191
case "/method/list":
199192
asyncHttpServerResponse.send("application/json; charset=utf-8", MethodUtil.listMethod(request).toJSONString());
200193
break;
@@ -245,9 +238,11 @@ public void complete(JSONObject data, Method method, MethodUtil.InterfaceProxy p
245238
}
246239
}
247240

241+
break;
242+
default:
243+
asyncHttpServerResponse.end();
248244
break;
249245
}
250-
251246
} catch (Exception e) {
252247
e.printStackTrace();
253248
send(asyncHttpServerResponse, MethodUtil.CALLBACK.newErrorResult(e).toJSONString());
@@ -272,6 +267,7 @@ public void run() {
272267
@Override
273268
protected void onDestroy() {
274269
isAlive = false;
270+
stop(etUnitPort);
275271
super.onDestroy();
276272
}
277273

0 commit comments

Comments
 (0)