十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
最近遇到一个需求需要在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); }