I am in the early stages of putting together an Android app that will host an assets based "website" using NanoHttpd. The app will rely heavily on the jquery library. I am using this NanoHttpd.java file and have successfully hosted and accessed a html file. My problem is that I cannot seem to be able to include a local copy of the jquery library. If I reference a cdn hosted copy, everything works just as it should. If I place a copy of the library in the assets directory, the html of the page displays correctly in the browser however, I receive the following error in the javascript console of chrome:
Resource interpreted as Script but transferred with MIME type text/html: "http://172.16.0.90:8080/jquery-1.11.0.min.js". 172.16.0.90/:1
Uncaught SyntaxError: Unexpected token <
My html and my extended WebServer class is posted below. What am I missing? Looking at the source of this project, tells me that this is possible however, I am stumped. Any help would be appreciated.
index.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<!-- making reference to local library in the root of the assets directory such as below results in specified error -->
<script src='jquery-1.11.0.min.js' type='text/javascript'></script>
<!-- making reference to cdn works just fine?? -->
<!--<script src='//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js' type='text/javascript'></script>-->
<script type="text/javascript">
$(document).ready(function(){
$('#ajax_test').click(function() {
alert('jquery is working');
});
});
</script>
</head>
<body>
<h1>hello from nanohttpd</h1><br />
<input type="button" id="ajax_test" value="ajax" />
</body>
</html>
WebServer.java
public class WebServer extends NanoHTTPD {
Context mContext;
public WebServer(int port, Context context) {
super(port);
mContext = context;
}
@Override public Response serve(IHTTPSession session) {
Method method = session.getMethod();
String uri = session.getUri();
String answer = "";
try {
AssetManager assetManager = mContext.getAssets();
InputStream inputStream = assetManager.open("index.html");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ((line = reader.readLine()) != null) {
answer += line;
}
reader.close();
} catch(IOException ioe) {
Log.w("Httpd", ioe.toString());
}
return new NanoHTTPD.Response(answer);
}
}
来源:https://stackoverflow.com/questions/25632029/android-nanohttpd-and-local-jquery-reference