NanoHTTPD是一个可以把移动端当成一个轻量级服务器的框架
使用也比较简单,在github搜索NanoHTTPD,添加依赖jar包即可(这里用的是2.3.2版本的)
接收get方法传过来的数据比较简单,网上搜一下很多
这里要说的是怎么接收post传过来的json数据?网上搜了一下也有很多,但是有很多坑
我实现的接收方法:
if (method.equals(Method.POST)){ Map<String, String> files = new HashMap<String, String>(); /*获取header信息,NanoHttp的header不仅仅是HTTP的header,还包括其他信息。*/ Map<String, String> header = session.getHeaders(); try { session.parseBody(files); String param=files.get("postData"); Log.d(TAG,"header : " + header); Log.d(TAG,"files : " + files); Log.d(TAG,"param : " + param); } catch (IOException e) { e.printStackTrace(); } catch (ResponseException e) { e.printStackTrace(); } }
其中的param就是传过来的json数据,或者post过来的任意字符串
files.get(postData),中postData 是框架内部自己定义的key 打印的files就可以看见
对header感兴趣的可以打印出来看看
网上好多文章没说到postData这个key,导致不知道如何获取post数据