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

1,036 阅读1分钟

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

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;
 
/**
 * @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(context);
instance.start();