Android,NanoHttpd和Local jQuery参考

时间:2014-09-02 20:42:15

标签: android jquery android-assets nanohttpd

我正处于使用NanoHttpd组建Android应用程序的早期阶段,该应用程序将托管基于资产的“网站”。该应用程序将严重依赖于jquery库。我正在使用此NanoHttpd.java文件并成功托管并访问了一个html文件。我的问题是我似乎无法包含jquery库的本地副本。如果我引用一个cdn托管副本,一切都可以正常工作。如果我将库的副本放在assets目录中,页面的html会在浏览器中正确显示,但是我在chrome的javascript控制台中收到以下错误:

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 < 

我的html和扩展的WebServer类发布在下面。我错过了什么?看一下this project的来源,告诉我这是可能的,但我很难过。任何帮助将不胜感激。

的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);
    }

}

0 个答案:

没有答案