我正在尝试使用 NanoHTTP 在 Android 的帮助下提供 HTML 文件。目前我能够显示页面。我也是安卓的新手。即使我在 AssetManager 的帮助下获取了文件,问题也没有清晰的图片如何引用该 html 文件的图像和 css。下面我提供了整个代码。有人可以帮我解决这个问题。
注意:其他权限设置很好,例如 InternetPermission。只是不知道如何为 html 引用其他支持的文件(css、图像、javascript 文件)。
package com.web;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import java.io.*;
import java.util.*;
public class MainActivity extends Activity
{
private WebServer server;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AssetManager assetManager = getAssets();
String imagePath = "fileapp/image";
String cssPath = "fileapp/css";
String files[] = null;
try {
files = assetManager.list(imagePath);
Log.i("Image", files[0]);
Log.w("Image", files[0]);
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("Image ",e.getMessage());
}
server = new WebServer();
try {
server.start();
} catch(IOException ioe) {
Log.w("Httpd", "The server could not start.");
}
Log.w("Httpd", "Web server initialized.");
}
// DON'T FORGET to stop the server
@Override
public void onDestroy()
{
super.onDestroy();
if (server != null)
server.stop();
}
private class WebServer extends NanoHTTPD {
public WebServer()
{
super(8080);
}
@Override
public Response serve(String uri, Method method,
Map<String, String> header,
Map<String, String> parameters,
Map<String, String> files) {
StringBuilder msg = new StringBuilder("<html><head><title>" + heading + "</title>" +
**"<link href=\"../../../fileapp/css/style.css\" rel=\"stylesheet\">" +**
"</head>"
+ "<body>");
msg.append("**<div class=\"jumbotron-sm\"> <img src=\"../../../fileapp/image/logo.png\">**</div>");
msg.append("<div>My Body Content go here......</div>");
msg.append("</body>"
+ "</html>");
return msg.toString();
}
}
}
如果有任何进一步的细节,请告诉我。
提前感谢您的回答。