赞
踩
使用该 lib,可以在 Android 设备上,架设 Http Server。客户端(Client) App,请求 http://[serverIp]:[server端口]/… 访问服务。适用于,局域网内多安卓设备(一台作为服务器,其它作为客户端)间通信。
https://github.com/NanoHttpd/nanohttpd
依赖:implementation 'org.nanohttpd:nanohttpd:2.3.1'
import android.util.Log import fi.iki.elonen.NanoHTTPD class MyHTTPDServer(port: Int) : NanoHTTPD(port) { private var isTest = true override fun serve(session: IHTTPSession): Response { return if (isTest) test(session) else NanoHTTPD.newFixedLengthResponse("msg") } private fun test(session: IHTTPSession): Response { var msg = "test" if (session.method == Method.GET) { if (session.uri.contains("testHtml")) { val map = decodeParameters(session.queryParameterString) val sb = StringBuilder() sb.append("method: GET").appendLine() map.forEach { k, u -> sb.append("key:$k, value:") u.forEach { sb.append("$it,") } sb.appendLine() } msg = sb.toString() Log.i("httpserver", "return msg: $msg") return NanoHTTPD.newFixedLengthResponse(msg) } } else if (session.method == Method.POST) { val map = mutableMapOf<String, String>() session.parseBody(map) session.parms?.forEach { Log.i("param", "serve: ${ it.key} : ${ it.value}") } if (session.uri.contains("beanData") && session.queryParameterString?.contains("imgData") == true) { Log.i("htt
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。