Android Http Server

Source: Internet
Author: User

Android Http Server
1 Introduction
How does Android build an Http server? The following is a small example in this article. In this example, mobile phone files can be viewed on a PC and downloaded or deleted.
 
2 Preparation
Now, the Http protocol is RFC2616. The basic knowledge is necessary. It is a good choice to obtain a reference to the protocol specification. Then you can start ^.
 
At first, I wanted to write an Http server to learn it, and then I was lazy. If you are interested, we recommend NanoHTTPD, which is a very small http server with only one class. However, only part of Http/1.1 is supported. Of course, there are more simple implementation examples, but the support is less and more simple.
Then, we use HttpService In the SDK. This part of the api is derived from the http core in the apache httpcomponents project. Because of the different SDK versions, there are many minor differences with the latest http core in terms of usage details. As for the example in this article, it is the implementation of api-8.
 
3. Create a Server
Create a WebServer and use HttpService to implement the server. It registers three Request Processing Methods: HttpFileHandler, HttpZipHandler, and HttpDelHandler. When the WebServer receives a connection and sends it to WorkerThread, the new thread processes each request.
 
Some WebServer code:
 
// Create an HTTP request executor Registry
HttpRequestHandlerRegistry reqistry = new HttpRequestHandlerRegistry ();
// Add an HTTP request executor
Reqistry. register ("*" + SUFFIX_ZIP, new HttpZipHandler (webRoot ));
Reqistry. register ("*" + SUFFIX_DEL, new HttpDelHandler (webRoot ));
Reqistry. register ("*", new HttpFileHandler (webRoot ));
// Set the HTTP request executor
HttpService. setHandlerResolver (reqistry );
/* Receive clients cyclically */
IsLoop = true;
While (isLoop &&! Thread. interrupted ()){
// Receive client socket
Socket socket = serverSocket. accept ();
// Bind the HTTP connection to the server
DefaultHttpServerConnection conn = new DefaultHttpServerConnection ();
Conn. bind (socket, params );
// Send to WorkerThread to process the request
Thread t = new WorkerThread (httpService, conn );
T. setDaemon (true); // set it to the daemon thread
T. start ();
}
 
4. Start Server
A WebService is created, and the WebServer is encapsulated to implement the Service mode. The interface is very simple, just a button, start as follows:

 
5. Access Server
Visit the above URL.
 

 

 
The above picture is available only when the directory exists and is readable. Otherwise, a page of 404 or 403 is reported. Add the delete option when writing. Return to the response page, which is processed in HttpFileHandler.
1) download
Folder: it is automatically packaged as zip for download. It is processed in HttpZipHandler.
File: automatically determines the type, or displays or downloads the file. HttpFileHandler does not process the data.
2) Delete
Will prompt whether to delete. If you confirm the deletion, the system will wait until the processing is completed and then confirm the automatic refresh.
 

 
The jquery impromptu plug-in is used. css and js related files are in the assets/wfs directory. CopyUtil provides a method to copy the assets Directory to the system directory.
 
6 postscript
Some time ago, I started to look at other things that are 'messy. A small example of Android may not write = again.

Author: winorlose2000

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.