GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - gpfduoduo/HttpServerOnAndroid: 在Android下几种实现HttpServer的方法 (以通过浏览器浏览sdcard内容为例说明)

BranchesTags
Open more actions menu

Latest commit

 

History

20 Commits
20 Commits

Folders and files

NameName
Last commit message
Last commit date
.idea
.idea
 
 
app
app
 
 
gradle/wrapper
gradle/wrapper
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
HttpServerOnAndroid.iml
HttpServerOnAndroid.iml
 
 
README.md
README.md
 
 
browse android sdcard file by http.gif
browse android sdcard file by http.gif
 
 
build.gradle
build.gradle
 
 
device-2015-09-05-220442.png
device-2015-09-05-220442.png
 
 
gradle.properties
gradle.properties
 
 
gradlew
gradlew
 
 
gradlew.bat
gradlew.bat
 
 
http core browse sdcard.PNG
http core browse sdcard.PNG
 
 
settings.gradle
settings.gradle
 
 

Repository files navigation

HttpServerOnAndroid

在Android下几种实现HttpServer的方法-总结

项目介绍

之前开源的项目DLNA和Airplay中都用到了HttpServer。
DLNA中需要实现HttpServer作为DMS等。
Airplay中需要实现HttpServer来接受苹果设备作为客户端推送的get和post消息。
因此现在总结以下HttpServer的实现方法。
以在PC中通过浏览器浏览sdcard中的内容为例说明http server的使用方法。

目前实现

通过在浏览器输入ip的方式,浏览你的手机中的文件及其文件夹(sdcard),对于文件,通过点击即可下载。

使用apache的HttpCore实现HttpServer

这应该是相对来说比较简单的方式,HttpCore将所有的http交互处理逻辑封装好了。
你只需要实现继承并实现HttpRequestHandler的handle方法,然后懂得使用httpRequest和httpResonse就可以实现一个较好的http server。
具体的:

  //设置 http 参数
            params = new BasicHttpParams();
            params.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
                    .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5 * 1000)
                    .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK,
                        false)
                    .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
                    .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "HttpComponents/1.1");

            BasicHttpProcessor httpProcessor = new BasicHttpProcessor();//http协议处理器
            httpProcessor.addInterceptor(new ResponseDate());//http协议拦截器,响应日期
            httpProcessor.addInterceptor(new ResponseServer());//响应服务器
            httpProcessor.addInterceptor(new ResponseContent());//响应内容
            httpProcessor.addInterceptor(new ResponseConnControl());//响应连接控制

            //http请求处理程序解析器
            HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry();

            //http请求处理程序,HttpFileHandler继承于HttpRequestHandler(http请求处理程序)
            registry.register(Constant.Http.BROWSE, new FileBrowseHandler(webRoot));
            registry.register(Constant.Http.DOWNLOAD, new FileDownLoadHandler(webRoot));

            httpService = new HttpService(httpProcessor,
                new DefaultConnectionReuseStrategy(), new DefaultHttpResponseFactory());
            httpService.setParams(params);
            httpService.setHandlerResolver(registry);//为http服务设置注册好的请求处理器。

手机端收到的浏览器的请求在FileBrowseHandler和FileDownLoadHandler中进行相应的处理即可。

效果图

手机端:
image

PC端浏览器:
image

About

在Android下几种实现HttpServer的方法 (以通过浏览器浏览sdcard内容为例说明)

Resources

Readme
Activity

Stars

90 stars

Watchers

3 watching

Forks

31 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.