Android NanoHTTPD快速使用

代码:

package chenlong.chenlong.autopylite;

import java.io.IOException;
import java.util.Map;
import java.util.logging.Handler;

import fi.iki.elonen.NanoHTTPD;

public class appServer extends NanoHTTPD {

    public static final int DEFAULT_SERVER_PORT = 8020;

    public appServer() throws IOException {
        super(DEFAULT_SERVER_PORT);
        start();
    }

    @Override
    public Response serve(IHTTPSession session) {
        String msg = "<html><body><h1>Hello AutoPy</h1>\n";
        Map<String, String> parms = session.getParms();
        if (parms.get("code") == null) {
            msg += "<form action='?' method='get'>\n  <p>Your code: <input type='text' name='code'></p>\n" + "</form>\n";
        } else {
            msg += "<p>Hello, " + parms.get("code") + "!</p>";
            return newFixedLengthResponse(msg + "</body></html>\n");
        }
        return newFixedLengthResponse(msg + "</body></html>\n");

    }
}

启动:

try {
            MyServer=new appServer();
        } catch (IOException e) {
            e.printStackTrace();
        }

 

posted @ 2020-10-10 15:25  sunny开始学坏  阅读(1786)  评论(0编辑  收藏  举报