android_http_server模块介绍
HttpServer模块速构建android上server服务器的方法。
1、如何使用
- 创建继承自
AndServerRequestHandler
的接口被调用时的处理类:public class CustomRequestHandler implements AndServerRequestHandler { @Override public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { //在这里声明接口被调用时的处理方法 }
- 获取HttpServerHandler的单例并进行配置:
customRequestHandler = new CustomRequestHandler(); handle = HttpServerHandler.getInstance(); //设置监听端口 handle.setHttpServerPort(1994); //设置超时时间 handle.setHttpServerTimeout(6000); //添加处理接口,第一个参数为自定义的url。如此设置后该处理方法的完整url为:http://ip:1994/custom handle.setHttpServerHandler("custom", customRequestHandler);
- 调用
handle.startHttpServer()
启动服务器; - 停止时调用
handle.stopHttpServer()
即可。
2、HttpServerUtil和ResponseMsg
HttpServerUtil中封装了一些数据处理的工具类:
isPostRequest //判断请求是否为post方法
isGetRequest //判断请求是否为get方法
parsePostRequest //解析post请求
parseGetRequest //解析get请求
sendResponse //生成response(StringMsg)
sendResponse //生成response(ResponseMsg)
ResponseMsg为默认的json返回值数据类,使用者可根据需求使用或自定义返回数据。
3、使用注意
使用需要权限
如何使用:
api 'com.sumavision.android_http_server:android_http_server:v1.x.x'
查看源码:
svn://192.165.152.13/sumard5/Project/Tetris/trunk/platform/android-platform/AndroidPlatform/android_http_server
This blog is under a CC BY-NC-SA 3.0 Unported License
本文链接:http://yeguli.cn/2018/01/08/android-http-server模块介绍/