"go" Android Http Server

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>Android Http Server</strong></p></p><strong><strong>1</strong></strong><strong><strong>Introduction</strong></strong>How does Android build an HTTP server? The small example of this article, about, or can make a reference ^ ^. well, The example implementation is the PC browsing the phone files, support the download and Delete.<strong><strong>2</strong></strong><strong><strong>Prepare</strong></strong>The HTTP protocol is still RFC2616, and its basic knowledge needs to be understood. It's a good choice to get a reference to a protocol Specification.          After that, you can start the ^ ^. At first, I want to write an HTTP server to learn, and later lazy = =. interested, recommended nanohttpd, very small only one class HTTP server, however, only partially supports http/1.1.         There are, of course, simpler examples of implementations, but there are fewer and more supported. In turn, it was implemented using the Httpservice in the Sdk. This part of the API originates from the HTTP core in the Apache httpcomponents project. Because of the different versions of the sdk, there are still a lot of small differences in the usage details with the latest HTTP Core. As for this example, it is the implementation on api-8.<strong><strong>3</strong></strong><strong><strong>Create</strong></strong><strong><strong>Server</strong></strong>Create webserver to implement the server Httpservice. It registered 3 requests for processing, httpfilehandler, httpziphandler, httpdelhandler. When webserver receives a connection, it distributes it to workerthread, and the new thread processes each Request. Webserver part of the Code:<span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>Create the HTTP request Executor Registry Httprequesthandlerregistry Reqistry = new Httprequesthandlerregistry (); Add HTTP request Executor Reqistry.register ("*" + suffix_zip, new Httpziphandler (webRoot)); Reqistry.register ("*" + suffix_del, new Httpdelhandler (webRoot)); Reqistry.register ("*", New Httpfilehandler (webRoot)); Set HTTP request Executor Httpservice.sethandlerresolver (reqistry); /* loop receive each client */isloop = true; While (isloop &&!) thread.interrupted ()) { //receive Client socket sockets Socket = serversocket.accept (); Bind to Server-side HTTP connection Defaulthttpserverconnection conn = new Defaulthttpserverconnection (); Conn.bind (socket, params); Dispatch to Workerthread processing request Thread t = new Workerthread (httpservice, conn); T.setdaemon (true); Set as Daemon thread </pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><strong><strong>4</strong></strong><strong><strong>Start</strong></strong><strong><strong>Server</strong></strong>Create a webservice, put webserver into the service Mode. The interface is simple, just a button that starts as Follows:<strong><strong>5</strong></strong><strong><strong>Access</strong></strong><strong><strong>Server</strong></strong>Notebook access to the above URL Chant. When the directory is present and readable, the above screen is available, otherwise it will simply report a page of 404 or 403. When writable, the delete option is Added.         Returns the response page, which is processed within httpfilehandler. 1) download Folder: will be automatically packaged into a zip Download.         It is processed within the Httpziphandler. File: automatically determine the type, or display or download.         Not much to deal with, still is Httpfilehandler processing. 2) Delete will prompt whether to Delete. If the deletion is confirmed, the processing end prompt will be completed, and then the automatic refresh is Confirmed. With the jquery impromptu plugin, the relevant css, JS in the Assets/wfs directory. Copyutil provides a way to replicate the assets directory to the system Directory.<strong><strong>6</strong></strong><strong><strong>PostScript</strong></strong>I started looking at other ' messy ' things a while ago. Android's Small example may not be able to write again = =.<strong><strong>Super Evolution Portal ^ ^</strong></strong><strong><strong>This article source: http://pan.baidu.com/s/1rIYz6</strong></strong><strong><strong>This article transferred from: http://vaero.blog.51cto.com/4350852/939413</strong></strong><strong><strong>from:http://www.cnblogs.com/sishuiliuyun/p/3461084.html</strong></strong><p><p>"go" Android Http Server</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.