Android嵌入式Web服务器 [英] Android embedded web-server

查看:259
本文介绍了Android嵌入式Web服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的android应用程序中嵌入一个网络服务器,其中连接到它的设备将具有两种功能.一种是发送一个我可以解析并做一些事情的xml文件,另一种是能够从我的sd卡中下载文件.

I want to embed a web-server in my android application where devices connecting to it will have two abilities. One is send an xml-file that I can parse and do some stuff and the other to be able to download a file from my sd-card.

我找到了nanoHTTPD和AndroidHTTPD等,但没有很好的文档,我遇到了问题,尤其是在如何提供下载文件的能力方面.除了我上面提到的文件以外,是否有任何可以处理文件或易于使用的库的优秀教程/示例?

I found nanoHTTPD and AndroidHTTPD etc but not good documentation and I'm facing problems especially on how to give the ability to download the File. Is there any good tutorial/example that handles files or an easy-to-use library except of the ones I noted above?

推荐答案

我想到了两件事,两者都是与权限相关的.看看您的android清单,您将需要两个权限给您的应用

Two things come to mind, both are permissions related. Take a look at your android manifest, you'll want two permissions for your app

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Internet,因为您正在访问网络服务,而WRITE_EXTERNAL_STORAGE,因为当NanoHttpd接收到传入的连接时,它将写入临时文件,并且大多数/所有电话都将"java.io.tmpdir"映射为指向SD卡.

INTERNET, because you're accessing network services, and WRITE_EXTERNAL_STORAGE because when NanoHttpd receives an incoming connection it writes temp files and most / all phones map the "java.io.tmpdir" to point at the SD card.

创建一个扩展基本NanoHttpd的类,传递要在其上运行的套接字,然后实现抽象的"serve()"方法.

Create a class that extends the basic NanoHttpd, passing the socket that you want to run on, and then implement the abstract 'serve()' method.

当您的客户端应用程序上载其XML文件时,请确保它以标准的Multipart表单编码发送,并且文件名将在"files"映射参数中提供给serve().

When your client application uploads their XML file, be sure that it's sending in the standard Multipart form encoding, and the filename will be given to you in the "files" map parameter to serve().

当您将响应发送回客户端时,新建一个Response()对象,然后可以将其交给 InputStream (来自SD卡文件),也可以仅提供内容的字符串

When you send a response back to a client, new up a Response() object, and you can either hand it an InputStream (from your SD card file) or just a String for the content.

如果您需要文档,则以示例形式,NanoHttpd包括一个功能齐全的Web服务器,用于提供文件,以及我称为"DemoServer"的服务器,它非常适合调试客户端应用程序发送的内容.目前还没有任何特定于Android的示例,但是大量的问题表明我应该添加一些示例. :)

If you need documentation, in the form of examples, NanoHttpd includes a fully functional webserver that serves files, and what I call "DemoServer" which is great for debugging what client applications send. There arent any Android specific examples yet but the volume of questions suggests that I ought to add some. :)

谢谢.希望这可以帮助.如果您还有其他问题,请告诉我.

Thanks. Hope this helps. Let me know if you have more questions.

这篇关于Android嵌入式Web服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆