上回咱們把基本的架子搭好了忙菠,不過也說了這個架子基本是不可用的,這回咱們就把它改造成基本可用的堕义。
3. 支持html文件
首先我們先實現對html的支持:
- 添加src/main/assets文件夾哀军,把html文件都放到這個文件夾下
- 修改server函數:
public Response serve(IHTTPSession session) {
String uri = session.getUri();
String filename = uri.substring(1);
if (uri.equals("/"))
filename = "index.html";
String response = "";
String line = "";
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(_mainContext.getAssets().open(filename)));
while ((line = reader.readLine()) != null) {
response += line;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return newFixedLengthResponse(response);
}
P.S.: 留個作業(yè)墨吓,為啥這里選assets文件夾呢媳纬?其他文件夾可以嗎双肤?
其實邏輯很簡單,就是找到想要打開的網頁文件钮惠,然后把文件內容讀出來返回茅糜。
看上去這個邏輯應該也能應付js和css對吧(image咱后面再說),但實際上你添加上js文件后萌腿,你會發(fā)現瀏覽器端是訪問不到的限匣,為啥呢抖苦?
這個咱得去nanoHTTPD的源碼里瞅瞅去毁菱。(別一說要看源碼就慌了,沒那么恐怖锌历,咱只看跟咱們問題相關的那幾行贮庞,然后再結合上猜就差不多了)
4. 淺入探索nanoHTTPD源碼
這里咱們用到一個newFixedLengthResponse函數,所以咱們首先看看這個函數是咋干活的:
public static Response newFixedLengthResponse(String msg) {
return newFixedLengthResponse(Status.OK, NanoHTTPD.MIME_HTML, msg);
}
就一行代碼究西,簡單明了窗慎,一個html的狀態(tài),一個html的類型卤材,一個咱剛剛傳入的html正文遮斥。再結合咱的js和css,應該能猜出問題出在MIME_HTML上扇丛,我們看下這個變量的定義:
public static final String MIME_HTML = "text/html";
如果你對html比較在行的話术吗,應該能猜出來js對應的這個值應該是:text/javascript,css對應的是:text/css對吧帆精,實際上在nanoHTTPD項目的源碼里也確實有這個定義的:
#default mime types for nanohttpd, use META-INF/mimetypes.properties for user defined mimetypes
css=text/css
htm=text/html
html=text/html
xml=text/xml
java=text/x-java-source, text/java
md=text/plain
txt=text/plain
asc=text/plain
gif=image/gif
jpg=image/jpeg
jpeg=image/jpeg
png=image/png
svg=image/svg+xml
mp3=audio/mpeg
m3u=audio/mpeg-url
mp4=video/mp4
ogv=video/ogg
flv=video/x-flv
mov=video/quicktime
swf=application/x-shockwave-flash
js=application/javascript
pdf=application/pdf
doc=application/msword
ogg=application/x-ogg
zip=application/octet-stream
exe=application/octet-stream
class=application/octet-stream
m3u8=application/vnd.apple.mpegurl
ts=video/mp2t
5. 支持js和css
本著代碼能工作较屿,絕不多學一點”的原則,我們再改下咱們的代碼:
public Response serve(IHTTPSession session) {
String uri = session.getUri();
System.out.println("####MyWebServer:" + uri);
String filename = uri.substring(1);
if (uri.equals("/"))
filename = "index.html";
String mimetype = "text/html";
if (filename.contains(".html") || filename.contains(".htm")) {
mimetype = "text/html";
} else if (filename.contains(".js")) {
mimetype = "text/javascript";
} else if (filename.contains(".css")) {
mimetype = "text/css";
} else {
filename = "index.html";
mimetype = "text/html";
}
String response = "";
String line = "";
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(_mainContext.getAssets().open(filename)));
while ((line = reader.readLine()) != null) {
response += line;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return newFixedLengthResponse(Response.Status.OK, mimetype, response);
}
OK卓练,app重啟隘蝎,瀏覽刷新!
是不是js和css也都OK了襟企。(不OK的同學嘱么,下課可以找我)
嗯,就剩下一個image顯示的問題了顽悼,咱一篇見曼振。