Android, NanoHttpd and Local jQuery reference

早过忘川 提交于 2019-12-02 16:29:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!