Android中的NanoHTTPD服务多个文件网页

时间:2014-06-24 15:39:21

标签: javascript android html nanohttpd

我在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

0 个答案:

没有答案