我在 Android 应用程序中使用 NanoHTTPD 来提供强制托管在服务器中的网页,因此我无法将其直接加载到 webview。
如果我加载单个 html 文件,则服务器和客户端都可以工作。
如果我随后将包含包含到 javascript 文件的 index.html 提供给每个包含的 js 文件,我会收到 Chromium (Crosswalk) 错误:
06-24 17:27:28.473: I/chromium(16287): [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected end of input", source: http://localhost:8080/interface.js (1)
我认为在这些情况下问题不在于给出正确的 MIME 类型,但我更改了代码以做到这一点,但它仍然失败:
private class WebServer extends NanoHTTPD {
public static final String MIME_JAVASCRIPT = "text/javascript";
public static final String MIME_CSS = "text/css";
public WebServer() {
super(8080);
}
@Override public Response serve(IHTTPSession session) {
String mime_type = NanoHTTPD.MIME_HTML;
Method method = session.getMethod();
String uri = session.getUri();
System.out.println(method + " '" + uri + "' ");
String answer = "";
if(method.toString().equalsIgnoreCase("GET")){
String path;
if(uri.equals("/")){
path="/index.html";
}else{
path = uri;
try{
if(path.substring(path.length()-2, path.length()).equalsIgnoreCase("js")){
mime_type = MIME_JAVASCRIPT;
}else if(path.substring(path.length()-3, path.length()).equalsIgnoreCase("css")){
mime_type = MIME_CSS;
}
}catch(Exception e){
}
}
try {
// Open file from SD Card
InputStream descriptor = getAssets().open("www/attitude"+path);
InputStreamReader index = new InputStreamReader(descriptor);
BufferedReader reader = new BufferedReader(index);
String line = "";
while ((line = reader.readLine()) != null) {
answer += line;
}
reader.close();
} catch(IOException ioe) {
Log.w("Httpd", ioe.toString());
}
}
return new NanoHTTPD.Response( Response.Status.OK,mime_type,answer);
}
}
我的 javascript 文件直接包含函数,例如 init.js:
function init()
{
setLoadingProgress(1);
//setLoadingProgress(15);
initScene();
//setLoadingProgress(35);
setLoadingProgress(5);
initReference();
initAngles();
//setLoadingProgress(65);
setLoadingProgress(10);
initIndicators();
//setLoadingProgress(75);
setLoadingProgress(13);
initSun();
//setLoadingProgress(85);
setLoadingProgress(15);
initEarth();
changeView(selected_view);
//setLoadingProgress(100);
setLoadingProgress(18);
}
除了 NanoHTTPD 之外,我还接受在与客户端相同的 Android 应用程序中提供多文件网页的更简单方法。
我唯一的目的是跳过服务器托管网页的限制,但我想许多其他人也想知道如何将这个类用于复杂的网页。
谢谢。
编辑 1:具有许多 MIME 类型检测的新代码
public static final String MIME_JAVASCRIPT = "text/javascript";
public static final String MIME_CSS = "text/css";
public static final String MIME_JPEG = "image/jpeg";
public static final String MIME_PNG = "image/png";
public static final String MIME_SVG = "image/svg+xml";
public static final String MIME_JSON = "application/json";
@Override public Response serve(IHTTPSession session) {
String mime_type = NanoHTTPD.MIME_HTML;
Method method = session.getMethod();
String uri = session.getUri();
System.out.println(method + " '" + uri + "' ");
InputStream descriptor = null;
if(method.toString().equalsIgnoreCase("GET")){
String path;
if(uri.equals("/")){
path="/index.html";
}else{
path = uri;
try{
if(path.endsWith(".js")){
mime_type = MIME_JAVASCRIPT;
}else if(path.endsWith(".css")){
mime_type = MIME_CSS;
}else if(path.endsWith(".html")){
mime_type = MIME_HTML;
}else if(path.endsWith(".jpeg")){
mime_type = MIME_JPEG;
}else if(path.endsWith(".png")){
mime_type = MIME_PNG;
}else if(path.endsWith(".jpg")){
mime_type = MIME_JPEG;
}else if(path.endsWith(".svg")){
mime_type = MIME_SVG;
}else if(path.endsWith(".json")){
mime_type = MIME_JSON;
}
}catch(Exception e){
}
}
try {
// Open file from SD Card
descriptor = getAssets().open("www/orbit"+path);
} catch(IOException ioe) {
Log.w("Httpd", ioe.toString());
}
}
return new NanoHTTPD.Response( Response.Status.OK,mime_type,descriptor);
}
它因以下错误而崩溃:
06-25 10:27:03.470: I/System.out(19604): GET '/Cesium/Workers/cesiumWorkerBootstrapper.js'
06-25 10:27:03.480: I/System.out(19604): GET '/Cesium/Workers/transferTypedArrayTest.js'
06-25 10:27:03.520: I/System.out(19604): GET '/Cesium/Assets/Textures/moonSmall.jpg'
06-25 10:27:03.550: I/System.out(19604): GET '/Cesium/Assets/Textures/SkyBox/tycho2t3_80_py.jpg'
06-25 10:27:03.550: I/System.out(19604): GET '/Cesium/Assets/Textures/SkyBox/tycho2t3_80_my.jpg'
06-25 10:27:03.550: I/System.out(19604): GET '/Cesium/Assets/Textures/SkyBox/tycho2t3_80_pz.jpg'
06-25 10:27:03.560: I/System.out(19604): GET '/Cesium/Assets/Textures/SkyBox/tycho2t3_80_mx.jpg'
06-25 10:27:03.560: I/System.out(19604): GET '/Cesium/Assets/Textures/SkyBox/tycho2t3_80_px.jpg'
06-25 10:27:03.570: I/System.out(19604): GET '/Cesium/Assets/Textures/SkyBox/tycho2t3_80_mz.jpg'
06-25 10:27:03.980: A/libc(19604): Fatal signal 11 (SIGSEGV) at 0x00000080 (code=1), thread 19696 (Chrome_InProcGp)
如果您在打开浏览器的调试控制台的情况下打开此示例,您将看到以下未执行的 GET。它是图像/png,但某些字段(例如状态)与其他字段不同。还有一些 .js 文件是 application/javascript 和其他 application/x-javascript
cesiumjs.org/Cesium/Build/HelloWorld.html