最近遇到一个需求需要在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) {
HashMap files = 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);
}

售后响应及时
7×24小时客服热线
数据备份
更安全、更高效、更稳定
价格公道精准
项目经理精准报价不弄虚作假
合作无风险
重合同讲信誉,无效全额退款