如何在Android中实现一个简易的Http服务器-创新互联
最近遇到一个需求需要在App中创建一个Http服务器供供浏览器调用,用了下开源的微型Htpp服务器框架:NanoHttpd,项目地址:https://github.com/NanoHttpd/nanohttpd
直接上代码
public class HttpServer extends NanoHTTPD { public HttpServer(int port) { super(port); } @Override public Response serve(IHTTPSession session) { HashMapfiles = new HashMap<>(); Method method = session.getMethod(); if (Method.POST.equals(method)) { try { //notice:If the post with body data, it needs parses the body,or it can't get the body data; session.parseBody(files); }catch (IOException e) { return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + e.getMessage()); }catch (ResponseException e) { return newFixedLengthResponse(e.getStatus(), MIME_PLAINTEXT, e.getMessage()); } } final String postData = files.get("postData"); String transJson = Transmit.getInstance().getAuthoriseData(postData); return newFixedLengthResponse(transJson); }
分享标题:如何在Android中实现一个简易的Http服务器-创新互联
转载源于:http://azwzsj.com/article/dcscse.html