android nano app,实战nanoHTTPD嵌入android app(3)

这里将告诉您实战nanoHTTPD嵌入android app(3),教程操作步骤:

这篇咱们来解决image显示的问题。

6. 支持image

看看2000年的网易,是多么的朴(chou)素(lou)。

8133.html

Image.png

所以支持主流的image也是必须滴!

有了上面的基础,其实加个图片也是个小case啦,也就是两个点:

image的类型。从上面那个表,咱们可以看到这几个,只要把它们加到咱们的if else里去,这个问题就解决了:

gif=image/gif

jpg=image/jpeg

jpeg=image/jpeg

png=image/png

如何把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 /d/file/shujuku/dru0mvmpr5s

作者:tonytalks 链接:/d/file/shujuku/4a3sqcgnio5 来源:简书 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

实战nanoHTTPD嵌入android app(3)就为您介绍到这里,感谢您关注懒咪学编程c.lanmit.com.

本文地址:https://c.lanmit.com/yidongkaifa/qita/8133.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值