JustWe-WebServer Android上的Http服务器

JustWe-WebServer

Android手机上的Http服务器,可以用于内网/外网的数据交换。
734080-20160116105935007-4524531.png

ps: 这个项目是JustWeEngine游戏框架中处理网络事件的一部分。

如何使用

设置as Library或直接将代码拷出,如果使用JustWeEngine可以直接使用Gradle或者Maven构建。

快速入门

1.添加Service:

使用前请先添加Service到manifest文件:


    <service android:name=".WebServer.WebServerService"/>
    

2.初始化\打开\关闭:

    
     private WebServer server;
     server = new WebServer(MainActivity.this, new OnLogResult() {
        @Override
        public void OnResult(String log) {
                
        }

        @Override
        public void OnError(String error) {

        }
     });
     server.initWebService();
     

初始化的时候推荐实现一个OnLogResult用于接受log日志和错误。
OnLogResult的返回数据是线程安全的,可以直接传送到View中打印出来。
当然也有其他的构造方法:


    public WebServer(Activity engine);
    public WebServer(Activity engine, OnLogResult logResult, int webPort); // 端口
    

初始化之后:


    server.startWebService();
    server.stopWebService();
    

使用该方法打开监听\关闭。

3.添加路由:


        server.apply("/lfk", new OnWebStringResult() {
            @Override
            public String OnResult() {
                return "=======";
            }
        });

        server.apply("/main", new OnWebFileResult() {
            @Override
            public File returnFile() {
                return new File(WebServerDefault.WebServerFiles+"/"+"welcome.html");
            }
        });
        

可以通过此种方法添加路由,并返回数据或者文件。
需要表单提交的如Post可以使用如下接口,返回一个HashMap存储key和value。


       server.apply("/lfkdsk", new OnPostData() {
           @Override
           public String OnPostData(HashMap<String, String> hashMap) {
               String S = hashMap.get("LFKDSK");
               Logger.e(S);
               return "==";
           }
       });
       

4.获取/提交数据:

向服务器提交数据,只需使用正常的get / post即可。

有问题反馈

在使用中有任何问题,欢迎反馈给我,可以用以下联系方式跟我交流

License

Copyright 2015 [刘丰恺](http://www.cnblogs.com/lfk-dsk/)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

转载于:https://www.cnblogs.com/lfk-dsk/p/5135148.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值