在Android設(shè)備上搭建Web服務(wù)器的方法
一般而言,Android 應(yīng)用在請求數(shù)據(jù)時都是以 Get 或 Post 等方式向遠(yuǎn)程服務(wù)器發(fā)起請求,那你有沒有想過其實(shí)我們也可以在 Android 設(shè)備上搭建一個小型 Web 服務(wù)器,并且實(shí)現(xiàn)常規(guī)的下載圖片、下載文件、提交表單等功能呢?
下面要介紹的就是如何在 Android 設(shè)備上搭建一個 Web 服務(wù)器,這個 Web 服務(wù)器的功能有如下幾點(diǎn):
- 接受客戶端文件上傳、下載文件
- 動態(tài) Http API,像 Java 的 Servlet 一樣寫接口
- 部署靜態(tài)網(wǎng)站,例如純Html,支持 JS、CSS、Image 分離
- 部署動態(tài)網(wǎng)站
這需要依賴一個開源庫來實(shí)現(xiàn):AndServer
AndServer 類似于 Apache 和 Tomcat,支持在同個局域網(wǎng)下的設(shè)備能夠以常規(guī)的網(wǎng)絡(luò)請求方式來向 Web 服務(wù)器請求數(shù)據(jù),只要指明 Web 服務(wù)器的 IP 地址和端口號即可
那么,這個 Web 服務(wù)器的用途有哪些呢?
說下我現(xiàn)在遇到的一個需求吧!需要實(shí)現(xiàn)兩臺設(shè)備(Android 或 ios 設(shè)備)在無網(wǎng)絡(luò)情況下進(jìn)行數(shù)據(jù)交流。本來是打算讓設(shè)備之間的交流通道以 Wifi 來鏈接,即某一臺設(shè)備連上另一臺設(shè)備的 Wiif 熱點(diǎn),這樣兩者之間就建立起了一條“通道”,之后通過建立 Socket 連接來獲取輸入輸出流,通過輸入輸出流來交換數(shù)據(jù)??墒沁@樣就需要處理好在高并發(fā)情況下的數(shù)據(jù)同步和解析問題,比較麻煩,而通過 AndServer 就可以直接套用項(xiàng)目已有的網(wǎng)絡(luò)請求框架,直接以網(wǎng)絡(luò)請求的方式來交流數(shù)據(jù),而服務(wù)端也較好的處理了并發(fā)問題
Gradle 遠(yuǎn)程依賴
implementation 'com.yanzhenjie:andserver:1.1.3'
搭建服務(wù)器
搭建服務(wù)器的方式很簡單,支持鏈?zhǔn)秸{(diào)用。指明服務(wù)器在本機(jī)的 IP 地址上監(jiān)聽,并指定端口號為 1995 ,并開放了三個接口分別用于:下載文件、下載圖片、Post表單
AndServer server = AndServer.serverBuilder()
.inetAddress(NetUtils.getLocalIPAddress()) //服務(wù)器要監(jiān)聽的網(wǎng)絡(luò)地址
.port(Constants.PORT_SERVER) //服務(wù)器要監(jiān)聽的端口
.timeout(10, TimeUnit.SECONDS) //Socket超時時間
.registerHandler(Constants.GET_FILE, new DownloadFileHandler()) //注冊一個文件下載接口
.registerHandler(Constants.GET_IMAGE, new DownloadImageHandler()) //注冊一個圖片下載接口
.registerHandler(Constants.POST_JSON, new JsonHandler()) //注冊一個Post Json接口
.filter(new HttpCacheFilter()) //開啟緩存支持
.listener(new Server.ServerListener() { //服務(wù)器監(jiān)聽接口
@Override
public void onStarted() {
String hostAddress = server.getInetAddress().getHostAddress();
Log.e(TAG, "onStarted : " + hostAddress);
ServerPresenter.onServerStarted(ServerService.this, hostAddress);
}
@Override
public void onStopped() {
Log.e(TAG, "onStopped");
ServerPresenter.onServerStopped(ServerService.this);
}
@Override
public void onError(Exception e) {
Log.e(TAG, "onError : " + e.getMessage());
ServerPresenter.onServerError(ServerService.this, e.getMessage());
}
})
.build();
開啟服務(wù)器
server.startup();
停止服務(wù)器
server.shutdown();
接口處理器
在注冊接口時,除了指明開放出來的 Url 地址外,還需要指明相應(yīng)的處理器,專門用于處理該接口的請求操作
開放出來的三個接口分別對應(yīng)于三個地址
public class Constants {
//服務(wù)端接口的端口號
public static final int PORT_SERVER = 1995;
public static final String GET_FILE = "/file";
public static final String GET_IMAGE = "/image";
public static final String POST_JSON = "/json";
}
··· .registerHandler(Constants.GET_FILE, new DownloadFileHandler()) //注冊一個文件下載接口 .registerHandler(Constants.GET_IMAGE, new DownloadImageHandler()) //注冊一個圖片下載接口 .registerHandler(Constants.POST_JSON, new JsonHandler()) //注冊一個Post Json接口 ···
例如,假設(shè)設(shè)備的 IP 地址是:192.168.0.101 ,那么在訪問 http://192.168.0.101:1995/file 接口時,請求操作就會由 DownloadFileHandler 來處理
下載文件
DownloadFileHandler 實(shí)現(xiàn)了 RequestHandler 接口,在 handle 方法中可以獲取到請求頭,表單數(shù)據(jù)這些信息,,通過注解聲明支持 Get 方式調(diào)用,在此直接返回一個文本文件用于下載
/**
* 作者:leavesC
* 時間:2018/4/5 16:30
* 描述:https://github.com/leavesC/AndroidServer
*/
public class DownloadFileHandler implements RequestHandler {
@RequestMapping(method = {RequestMethod.GET})
@Override
public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
File file = createFile();
HttpEntity httpEntity = new FileEntity(file, ContentType.create(getMimeType(file.getAbsolutePath()), Charset.defaultCharset()));
httpResponse.setHeader("Content-Disposition", "attachment;filename=File.txt");
httpResponse.setStatusCode(200);
httpResponse.setEntity(httpEntity);
}
private File createFile() {
File file = null;
OutputStream outputStream = null;
try {
file = File.createTempFile("File", ".txt", MainApplication.get().getCacheDir());
outputStream = new FileOutputStream(file);
outputStream.write("leavesC,這是一段測試文本".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return file;
}
}
這里直接在瀏覽器中訪問接口(要和 Android Web服務(wù)器運(yùn)行在同個局域網(wǎng)下),可以直接下載到文件

下載圖片
類似的,下載圖片的接口處理器 DownloadImageHandler 可以如下設(shè)計(jì),在 handle 方法中返回應(yīng)用的圖標(biāo)
/**
* 作者:leavesC
* 時間:2018/4/5 16:30
* 描述:https://github.com/leavesC/AndroidServer
*/
public class DownloadImageHandler extends SimpleRequestHandler {
private File file = new File(Environment.getExternalStorageDirectory(), "leavesC.jpg");
@RequestMapping(method = {RequestMethod.GET})
@Override
protected View handle(HttpRequest request) throws HttpException, IOException {
writeToSdCard();
HttpEntity httpEntity = new FileEntity(file, ContentType.create(getMimeType(file.getAbsolutePath()), Charset.defaultCharset()));
return new View(200, httpEntity);
}
private void writeToSdCard() throws IOException {
if (!file.exists()) {
synchronized (DownloadImageHandler.class) {
if (!file.exists()) {
boolean b = file.createNewFile();
if (!b) {
throw new IOException("What broken cell phone.");
}
Bitmap bitmap = BitmapFactory.decodeResource(MainApplication.get().getResources(), R.mipmap.ic_launcher_round);
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
}
}
}
}
}
}

Post表單
這里需要將注解值改為 RequestMethod.POST,通過 HttpRequestParser.getContentFromBody(httpRequest) 函數(shù)可以獲取到表單數(shù)據(jù),這里直接檢測表單數(shù)據(jù)是否為 Json 字符串,是的話則為之多添加一個屬性 :"state" 作為返回值,否則返回只包含屬性 “state” 的 Json 字符串
/**
* 作者:leavesC
* 時間:2018/4/5 16:30
* 描述:https://github.com/leavesC/AndroidServer
*/
public class JsonHandler implements RequestHandler {
@RequestMapping(method = {RequestMethod.POST})
@Override
public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
String content = HttpRequestParser.getContentFromBody(httpRequest);
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(content);
} catch (JSONException e) {
e.printStackTrace();
}
if (jsonObject == null) {
jsonObject = new JSONObject();
}
try {
jsonObject.put("state", "success");
} catch (JSONException e) {
e.printStackTrace();
}
StringEntity stringEntity = new StringEntity(jsonObject.toString(), "utf-8");
httpResponse.setStatusCode(200);
httpResponse.setEntity(stringEntity);
}
}
這里在 Postman 這個工具上進(jìn)行 Post 操作

以上三個例子都是在電腦端調(diào)用的,這和在手機(jī)端調(diào)用是同個效果的
基本的操作就介紹到這里,再具體的內(nèi)容可以看示例代碼:AndroidServer
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 不同Activity間數(shù)據(jù)的傳遞 Bundle對象的應(yīng)用
本篇文章小編為大家介紹,Android 不同Activity間數(shù)據(jù)的傳遞 Bundle對象的應(yīng)用。需要的朋友參考下2013-04-04
Android簡單記錄和恢復(fù)ListView滾動位置的方法
這篇文章主要介紹了Android簡單記錄和恢復(fù)ListView滾動位置的方法,涉及Android針對ListView位置屬性的相關(guān)操作技巧,需要的朋友可以參考下2016-08-08
GridView實(shí)現(xiàn)桌面圖標(biāo)顯示案例
這篇文章主要為大家詳細(xì)介紹了GridView實(shí)現(xiàn)桌面圖標(biāo)顯示案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08
Android編程實(shí)現(xiàn)將tab選項(xiàng)卡放在屏幕底部的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)將tab選項(xiàng)卡放在屏幕底部的方法,涉及Android界面布局、tab設(shè)置及權(quán)限控制相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
vscode通過wifi調(diào)試真機(jī)的Flutter應(yīng)用的教程
這篇文章主要介紹了vscode通過wifi調(diào)試真機(jī)的Flutter應(yīng)用的教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
Flutter開發(fā)實(shí)現(xiàn)底部留言板
這篇文章主要為大家詳細(xì)介紹了Flutter開發(fā)實(shí)現(xiàn)底部留言板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
Android自定義View實(shí)現(xiàn)閃耀字體效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)閃耀字體效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01

