Actual nanoHTTPD embedded android app

Posted by riespies on Tue, 06 Aug 2019 05:54:25 +0200

Let's solve the problem of image display.

6. Supporting image

Look at 2000 Netease, how simple (chou) element (lou).

Image.png

So it's also necessary to support mainstream image ry!

With the above foundation, in fact, adding a picture is also a small case, that is, two points:

  1. The type of image. From the table above, we can see these, as long as we add them to our if El se, the problem will be solved:
gif=image/gif
jpg=image/jpeg
jpeg=image/jpeg
png=image/png
  1. How to Return image Content

Neither will I, but we will Google. Searching for this answer in stack overflow is also unusually simple, because nanoHTTPD has a function:

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

With this, what are we afraid of? Create an InputStream for it directly, and we can accomplish our task.

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, "");
}

Here we have implemented the basic functions of an http server.

7. Summarize

Through this small project, we can see that the open source world is full of interesting things, as long as you can find them. However, it will also be found that most of these things are semi-finished products, such as the official example does not provide how to support access to external html, js, css, image of these codes, they need to be transformed into their own code through their own hands.

Finally, I will send you all the MyServer code:

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, "");
        }
    }
}

If this article is helpful to your little friends, please take it. You are welcome.
If you want to forward it, just remember to bring my name.

Reference material:

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



Author: Tony talks
Link: https://www.jianshu.com/p/f54aa5c9d7a8
Source: Brief Book
The copyright of the brief book belongs to the author. For any form of reprinting, please contact the author for authorization and indicate the source.

Topics: Mobile Session Android Google Javascript