1. 程式人生 > >實戰nanoHTTPD嵌入android app(3)

實戰nanoHTTPD嵌入android app(3)

這篇咱們來解決image顯示的問題。


6. 支援image

看看2000年的網易,是多麼的樸(chou)素(lou)。

Image.png

所以支援主流的image也是必須滴!

有了上面的基礎,其實加個圖片也是個小case啦,也就是兩個點:

  1. image的型別。從上面那個表,咱們可以看到這幾個,只要把它們加到咱們的if else裡去,這個問題就解決了:
gif=image/gif
jpg=image/jpeg
jpeg=image/jpeg
png=image/png
  1. 如何把image的內容返回

這個問題我也不會,不過咱會谷歌。經過搜尋在stackoverflow裡找到這個答案,其實也異常的簡單,因為nanoHTTPD有一個函式:

public static Response newFixedLengthResponse(IStatus status, String mimeType, InputStream data, long totalBytes) {
    return new Response(status, mimeType, data, totalBytes);
}

有了這個,咱還怕啥,直接創建出一個InputStream給它,就能完成咱們的任務了:

InputStream isr;

try {
    isr = _mainContext.getAssets().open(filename);
    return newFixedLengthResponse(Response.Status.OK, mimetype, isr, isr.available());
} catch (IOException e) {
    e.printStackTrace();
    return newFixedLengthResponse(Response.Status.OK, mimetype, "");
}

到這裡咱們已經把一個http server要提供的基本功能都實現了。

7. 總結一下

經過這個小專案,咱們能看到開源的世界充滿了各種好玩的東西,只要你能找得到。不過也會發現這些東西大多都是個半成品,比如官方的例子裡就沒有提供如何支援訪問外部html,js,css,image這些的程式碼,都需要經過自己的手把它們改造成適合自己的程式碼。

最後奉上MyServer全部程式碼:

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";

    boolean is_ascii = true;
    String mimetype = "text/html";
    if (filename.contains(".html") || filename.contains(".htm")) {
        mimetype = "text/html";
        is_ascii = true;
    } else if (filename.contains(".js")) {
        mimetype = "text/javascript";
        is_ascii = true;
    } else if (filename.contains(".css")) {
        mimetype = "text/css";
        is_ascii = true;
    } else if (filename.contains(".gif")) {
        mimetype = "text/gif";
        is_ascii = false;
    } else if (filename.contains(".jpeg") || filename.contains(".jpg")) {
        mimetype = "text/jpeg";
        is_ascii = false;
    } else if (filename.contains(".png")) {
        mimetype = "image/png";
        is_ascii = false;
    } else {
        filename = "index.html";
        mimetype = "text/html";
    }

    if (is_ascii) {
        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);
    } else {
        InputStream isr;
        try {
            isr = _mainContext.getAssets().open(filename);
            return newFixedLengthResponse(Response.Status.OK, mimetype, isr, isr.available());
        } catch (IOException e) {
            e.printStackTrace();
            return newFixedLengthResponse(Response.Status.OK, mimetype, "");
        }
    }
}

如果這篇文章對各位小夥伴有幫助,敬請拿去,不用客氣。
如果要轉發,記得帶上我的名字就好了。

參考資料:

http://programminglife.io/android-http-server-with-nanohttpd/
https://github.com/NanoHttpd/nanohttpd/blob/master/core/src/main/resources/META-INF/nanohttpd/default-mimetypes.properties
https://stackoverflow.com/questions/24390864/nanohttpd-in-android-serving-multiple-files-webpage



作者:tonytalks
連結:https://www.jianshu.com/p/f54aa5c9d7a8
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯絡作者獲得授