android 本地server androidasync依赖库的简单使用

不在赘述什么是androidasync 有兴趣的话可以自己百度搜索一下

直接贴上代码 每个方法都有注释

import android.content.Context;
import android.widget.Toast;

import com.koushikdutta.async.http.server.AsyncHttpServer;
import com.koushikdutta.async.http.server.AsyncHttpServerRequest;
import com.koushikdutta.async.http.server.AsyncHttpServerResponse;
import com.koushikdutta.async.http.server.HttpServerRequestCallback;

import org.json.JSONException;
import org.json.JSONObject;

import cn.zjq.helpinstall.utils.InstallApkUtils;

/**
 * @Description: HttpServer简单使用
 * @Author: zjq
 * @CreateDate: 2020/12/18 15:43
 */
public class TestHttpServer implements HttpServerRequestCallback {
    AsyncHttpServer mServer = new AsyncHttpServer();
    private Context context;
    private static TestHttpServer testHttpServer;

    public static TestHttpServer getInstance() {
        if (testHttpServer == null) {
            synchronized (TestHttpServer.class) {
                if (testHttpServer == null) {
                    testHttpServer = new TestHttpServer();
                }
            }
        }
        return testHttpServer;
    }

    /**
     * 初始化server
     *
     * @param context
     */
    public void initServer(Context context) {
        this.context = context;
    }

    /**
     * 启动服务
     * 9000为监听的端口号
     */
    public void start() {
        Toast.makeText(context, "启动服务成功", Toast.LENGTH_SHORT).show();
        mServer.listen(9000);
        mServer.get("/install", this);
        mServer.post("/install", this);
    }

    /**
     * 实现 HttpServerRequestCallback接口方法
     *
     * @param request
     * @param response
     */
    @Override
    public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
        String method = request.getMethod();
        if ("GET".equalsIgnoreCase(method)) {
            doGetMethod(request, response);
        } else if ("POST".equalsIgnoreCase(method)) {
            doPostMethod(request, response);
        }
    }

    /**
     * 处理POST请求的方法
     *
     * @param request  请求
     * @param response 响应
     */
    private void doPostMethod(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {

    }

    /**
     * 处理GET请求的方法 监听请求install接口
     *
     * @param request
     * @param response
     */
    private void doGetMethod(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
        if ("/install".equalsIgnoreCase(request.getPath())) {
            handleDevicesRequest(response);
        } else {
            handleInvalidRequest(response);
        }
    }

    /**
     * 处理无效的请求
     *
     * @param response
     */
    private void handleInvalidRequest(AsyncHttpServerResponse response) {
        try {
            JSONObject json = new JSONObject();
            try {
                json.put("error", "Invalid API");
                response.send(json);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 响应设备有效的请求
     *
     * @param response
     */
    private void handleDevicesRequest(AsyncHttpServerResponse response) {
        //此处InstallApkUtils为本地工具类,可忽略
        InstallApkUtils instance = InstallApkUtils.getInstance();
        instance.initInstall(context);
        String result = instance.installApkMethod();
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", "200");
            jsonObject.put("err", "null");
            jsonObject.put("msg", result);
            response.send(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

测试方法

1.AS运行此程序到手机

2.手机和电脑连接至统一wifi

3.在浏览器中输入相对应的手机ip地址与相应端口

如下  我的手机ip地址为192.168.3.1 浏览器中输入

192.168.3.1:9000/install 

成功即可返回对应json

调用方法

TestHttpServer instance = TestHttpServer.getInstance();
instance.initServer(YibaService.this);
instance.start();

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值