当前位置:   article > 正文

Android 做服务器NanoHTTPD使用_android studio nanohttpd

android studio nanohttpd

Android 应用内架设服务使用NanoHTTPD

github地址点击打开链接找到releases下载相应jar包导入到Android studio 中,添加网络权限,创建类继承NanoHTTPD,重写serve方法即可

  1. public class HttpServer extends NanoHTTPD {
  2. public static final int DEFAULT_SERVER_PORT = 8020;
  3. private static final String REQUEST_ROOT = "/";
  4. private Handler mSelfHandler;
  5. private respBody mRespBody;
  6. private CutTicketDeviceThread mCutTicketDeviceThread;
  7. public HttpServer(Thread mThread) {
  8. super(DEFAULT_SERVER_PORT);
  9. //mylog.setFilename("HttpServer.log");
  10. mCutTicketDeviceThread = (CutTicketDeviceThread) mThread;
  11. mylog.debug(String.format("HttpServer创建 port=%d", DEFAULT_SERVER_PORT));
  12. }
  13. @Override
  14. public Response serve(IHTTPSession session) {
  15. Map<String, String> header = session.getHeaders();
  16. if ("127.0.0.1".equals(header.get("http-client-ip")) & "/sale".equals(session.getUri())) {
  17. return responseSale(session);
  18. }
  19. if (REQUEST_ROOT.equals(session.getUri())) {
  20. mylog.error("客户端请求路径错误:" + REQUEST_ROOT);
  21. return responseRootPage(session);
  22. }
  23. mylog.error("客户端请求路径错误:" + session.getUri());
  24. return response404(session, session.getUri());
  25. }
  1. public Response response404(IHTTPSession session, String url) {
  2. StringBuilder builder = new StringBuilder();
  3. builder.append("<!DOCTYPE html><html><body>");
  4. builder.append("Sorry, Can't Found " + url + " !");
  5. builder.append("</body></html>\n");
  6. mylog.error(" return 404");
  7. return new Response(builder.toString());
  8. }
}

利用server启动服务

  1. public class HttpService extends Service {
  2. private HttpServer mHttpServer;
  3. private boolean bServerStarted;
  4. private static Lock lock = new ReentrantLock();
  5. @Override
  6. public void onCreate()
  7. {
  8. bServerStarted=false;
  9. mylog.info( "HttpService Create.");
  10. return;
  11. }
  12. @Override
  13. public int onStartCommand(Intent intent, int flags, int startId) {
  14. startServer();
  15. return START_STICKY;
  16. }
  17. @Override
  18. public void onDestroy() {
  19. stopServer();
  20. super.onDestroy();
  21. }
  22. /**
  23. * Start server.
  24. */
  25. private void startServer() {
  26. if (mHttpServer != null && !bServerStarted)
  27. {
  28. try {
  29. mHttpServer.start();
  30. bServerStarted = true;
  31. }catch(Exception e){
  32. e.printStackTrace();
  33. }
  34. }else{
  35. if(mHttpServer!=null)
  36. {
  37. mylog.info(String.format("Http服务已经启动了 mHttpServer=%s bServerStarted=%s ....", mHttpServer.toString(), bServerStarted ? "true" : "false"));
  38. }
  39. }
  40. return;
  41. }
  42. /**
  43. * Stop server.
  44. */
  45. private void stopServer() {
  46. if (mHttpServer != null)
  47. {
  48. try {
  49. mHttpServer.stop();
  50. bServerStarted = false;
  51. mylog.info( "mHttpServer Stop().....");
  52. }catch(Exception e){
  53. e.printStackTrace();
  54. }
  55. }
  56. return;
  57. }
  58. @Nullable
  59. @Override
  60. public IBinder onBind(Intent intent) {
  61. return null;
  62. }
  63. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/611179
推荐阅读
相关标签
  

闽ICP备14008679号