I am using NanoHTTPD in an Android application to serve a webpage that is forced to be hosted in a server, so I can't load it directly to the webview.
If I load a single html file, the server and client works.
If I then serve an index.html with includes to javascript files I get an error of Chromium (Crosswalk) per included js file:
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)
I thought that the problem was not giving the correct MIME type in these cases but I changed the code in order to do that but it stills failing:
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);
}
}
My javascript files contain directly functions, e.g. 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);
}
I would accept also simpler ways to serve a multi-file webpage in the same Android application as the client other than NanoHTTPD.
My only purpose is to skip the restriction of having a server hosting the webpage, but I guess many other people would want to know also how to use this class with complex webpages.
Thanks.
Edit 1: New code with many MIME type detection
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);
}
It crashes with this error:
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)
If you open this example with the debug console of the browser opened you will see which is the following GET that is not executed. It is an image/png but some of the fields like the status are different to otherones. Also there are some .js files that are application/javascript and others application/x-javascript
cesiumjs.org/Cesium/Build/HelloWorld.html
User contributions licensed under CC BY-SA 3.0