java http服务器 框架_GitHub - chenxianfu/AndServer: Android端搭建简单的Http服务器的框架,傻瓜式构建,和Java的Servlet一样。...

AndServer

AndServer is a Web server of Android, support for the deployment of static website, dynamic website, support dynamic interface (Equivalent to Servelt of java ).

Features

Dynamic website deployment.

Static website deployment.

Dynamic http API.

Upload file to android application.

Download file from android application.

Dependencies

Gradle

compile 'com.yanzhenjie:andserver:1.0.2'

Maven

com.yanzhenjie

andserver

1.0.2

pom

Usage

The best tutorial is sample, recommended download preview.

Create Server

AndServer andServer = new AndServer.Build()

...

.build();

// Create server.

Server mServer = andServer.createServer();

...

// Start server.

mServer.start();

...

// Stop server.

mServer.stop();

...

// Server is running ?

boolean running = mServer.isRunning();

Port & Response Timeout

AndServer andServer = new AndServer.Build()

.port(8080) // Default 8080, can be any port.

.timeout(10 * 1000) // But for MS, the default is 10 * 1000 ms.

...

.build();

...

Website deployment

The deployment website is through the Website interface, you can implement it. AndServer provides two default implementations:

Through their registered website, the default home page is:

http://ip:port/

http://ip:port/youPath

http://ip:port/youPath/index.html

Register the website

Wesite wesite = new AssetsWebsite(AssetManager, youPath);

// or

Wesite wesite = new StorageWebsite(youPath);

AndServer andServer = new AndServer.Build()

...

.website(wesite);

.build();

AssetsWebsite usage

Use it when your static site content is placed under assets.

The way to use is:

AssetManager mAssetManager = getAssets(); //AssetManager can not be closed.

Wesite wesite = new AssetsWebsite(mAssetManager, youPath);

If the website root directory under the assets, then the incoming "", such as:

b3c2957c2d6783c4d22a084f0441289d.png

Wesite wesite = new AssetsWebsite(mAssetManager, "");

Then your default home page is:

http://ip:port

http://ip:port/index.html

Other page addresses is:

http://ip:port/login.html

http://ip:port/error.html

For example:

http://192.168.1.12:8080/index.html

http://192.168.1.12:8080/login.html

If the root directory of the site is in the subdirectory of assets, then the relative path to the directory, such as the site in the assets /web/website directory, such as:

web_assets_son.png

Wesite wesite = new AssetsWebsite(mAssetManager, "web/www");

Then your default home page is:

http://ip:port

http://ip:port/web/www

http://ip:port/web/www/index.html

Other page addresses is:

http://ip:port/web/www/login.html

http://ip:port/web/www/error.html

For example:

http://192.168.1.12:8080/

http://192.168.1.12:8080/index.html

http://192.168.1.12:8080/web/www/index.html

http://192.168.1.12:8080/web/www/index.html

http://192.168.1.12:8080/web/www/login.html

StorageWebsite usage

Use it when your static site is on a storage device, such as your site under SD card.

The way to use is:

Wesite wesite = new StorageWebsite(youPath);

It is simple, the absolute path into your website root directory on it, for example, your site in the SD card under the www directory:

File file = new File(Environment.getExternalStorageDirectory(), "www");

String websiteDirectory = file.getAbsolutePath();

Wesite wesite = new StorageWebsite(websiteDirectory);

Access address and AssetsWebsite the same reason.

Http API

Http API is through the RequestHandler interface registration, it is a java interface, it's the same as JavaEE's Servlet.

You need to implement it, and then registered to AndServer on it, such as:

public class RequestLoginHandler implements RequestHandler {

@Override

public void handle(HttpRequest req, HttpResponse res, HttpContext con) {

Map params = HttpRequestParser.parse(request);

// Request params.

String userName = params.get("username");

String password = params.get("password");

if ("123".equals(userName) && "123".equals(password)) {

StringEntity stringEntity = new StringEntity("Login Succeed", "utf-8");

response.setEntity(stringEntity);

} else {

StringEntity stringEntity = new StringEntity("Login Failed", "utf-8");

response.setEntity(stringEntity);

}

}

}

And then register it in AndServer:

AndServer andServer = new AndServer.Build()

...

.registerHandler("login", new RequestLoginHandler())

.build();

Now you can get a unique access address: http://ip:port/login, for example:

http://192.168.1.12:8080/login?username=123&password=123

For example of file download and file upload, please refer to sample.

Html submit form

In the action of the Html's form fill in you register RequestHandler key, and then you can get the form parameters in the handle(HttpRequest, HttpResponse, HttpContext) method of the RequestHandler.

Such as the top of the Login RequestHandler:

...

Listens on server status

private Server.Listener mListener = new Server.Listener() {

@Override

public void onStarted() {

// The server started successfully.

}

@Override

public void onStopped() {

// The server stops.

}

@Override

public void onError(Exception e) {

// There was an error when starting the server, usually the port was occupied.

}

};

AndServer andServer = new AndServer.Build()

...

.listener(mListener)

.build();

#License

Copyright 2017 Yan Zhenjie

Licensed under the Apache License, Version 2.0 (the "License");

you may not use this file except in compliance with the License.

You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值