java - Android AssetServer 扩展 NanoHTTPD

标签 java android cordova nanohttpd

我正在开发一个扩展 NanoHTTPD 的 AssetServer,以便访问基于 file://的页面不可用的 Javascript 功能。

这是我目前的代码:

class AssetServer extends NanoHTTPD{

    private Activity activity;

    public AssetServer(int port, Activity activity)
    {
        super(port);
        this.activity = activity;
    }

    @Override
    public Response serve(IHTTPSession session){

        String mime = "text/plain";

        InputStream is = null;
        String path = "www" + session.getUri();
        System.out.println("nanohttpd: serving " + path);
        String response = null;

        try{

            is = activity.getAssets().open(path);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();

            response = new String(buffer);

        }catch(IOException ioe){
            System.err.println("nanohttpd: error: " + ioe);
        }

        Response res = new Response(Response.Status.OK, mime, response);
        return res;

    }

}

在我的手机上运行 Android 应用程序时,logcat 输出表明该页面收到了页面请求的一些(但不是全部)文件。我的 loadUrl 调用有五秒钟的延迟,让服务器有时间在提供页面之前预热。

这是 logcat 输出:

04-08 19:19:54.548: I/CordovaLog(16411): Found start page location: index.html
04-08 19:19:54.553: D/Whitelist(16411): Unlimited access to network resources
04-08 19:19:54.553: D/CordovaActivity(16411): Resuming the App
04-08 19:19:54.553: D/CordovaActivity(16411): CB-3064: The errorUrl is null
04-08 19:19:54.568: D/dalvikvm(16411): GC_CONCURRENT freed 252K, 17% free 7664K/9156K, paused 4ms+9ms, total 33ms
04-08 19:19:54.578: D/webcore(16411):  CORE loadUrl: called
04-08 19:19:54.578: D/webkit(16411): Firewall not null
04-08 19:19:54.578: D/webkit(16411): euler: isUrlBlocked = false
04-08 19:19:54.588: D/SoftKeyboardDetect(16411): Ignore this event
04-08 19:19:54.673: D/libEGL(16411): loaded /system/lib/egl/libEGL_mali.so
04-08 19:19:54.683: D/libEGL(16411): loaded /system/lib/egl/libGLESv1_CM_mali.so
04-08 19:19:54.683: I/System.out(16411): nanohttpd: serving www/index.html
04-08 19:19:54.693: D/libEGL(16411): loaded /system/lib/egl/libGLESv2_mali.so
04-08 19:19:54.698: E/(16411): Device driver API match
04-08 19:19:54.698: E/(16411): Device driver API version: 20
04-08 19:19:54.698: E/(16411): User space API version: 20 
04-08 19:19:54.698: E/(16411): mali: REVISION=Linux-r3p2-01rel2 BUILD_DATE=Mon Sep  2 14:16:28 KST 2013 
04-08 19:19:54.733: D/OpenGLRenderer(16411): Enabling debug mode 0
04-08 19:19:54.738: D/WebView(16411): onSizeChanged - w:480 h:762
04-08 19:19:54.738: D/CordovaActivity(16411): onMessage(onPageStarted,http://localhost:16086/index.html)
04-08 19:19:54.788: D/WritingBuddyImpl(16411): getCurrentWritingBuddyView() 
04-08 19:19:54.798: I/System.out(16411): nanohttpd: serving www/lib/jquery-2.0.2.min.js
04-08 19:19:54.833: D/dalvikvm(16411): GC_CONCURRENT freed 100K, 16% free 7978K/9404K, paused 8ms+3ms, total 35ms
04-08 19:19:54.863: D/SoftKeyboardDetect(16411): Ignore this event
04-08 19:19:55.198: I/GATE(16411): <GATE-M>DEV_ACTION_COMPLETED</GATE-M>
04-08 19:19:55.198: D/CordovaWebViewClient(16411): onPageFinished(http://localhost:16086/index.html)
04-08 19:19:55.198: D/CordovaActivity(16411): onMessage(onPageFinished,http://localhost:16086/index.html)
04-08 19:19:57.213: D/CordovaActivity(16411): onMessage(spinner,stop)
04-08 19:19:57.243: D/TilesManager(16411): Starting TG #0, 0x539b0050
04-08 19:19:57.243: D/TilesManager(16411): new EGLContext from framework: 52aeba78 
04-08 19:19:57.243: D/GLWebViewState(16411): Reinit shader
04-08 19:19:57.283: D/GLWebViewState(16411): Reinit transferQueue

所提供的文件并不总是相同的,这表明存在并发问题。

有人知道我做错了什么吗? 提前致谢!

最佳答案

embedhttp非常适合这种情况。

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    super.init();
    // Set by <content src="index.html" /> in config.xml

    final Activity activity = this;
    HttpServer server = new HttpServer();
    server.addRequestHandler(new HttpRequestHandler() {
        @Override
        public HttpResponse handleRequest(HttpRequest request) {
            InputStream is = null;
            String path = "www" + request.getUri();
            String response = null;

            try{

                is = activity.getAssets().open(path);
                int size = is.available();
                byte[] buffer = new byte[size];
                is.read(buffer);
                is.close();

                response = new String(buffer);
            }catch(IOException ioe){
                ioe.printStackTrace();
            }
            System.out.println("embedhttp: serving: " + path + ", " + response.length() + "B");
            return new HttpResponse(HttpStatus.OK, response);
        }
    });

    try{
        server.bind(16086);
    }catch(IOException ioe){
        ioe.printStackTrace();
    }

    server.start();
    //super.loadUrl(Config.getStartUrl());
    super.loadUrl("http://localhost:16086/index.html", 5000);
}

关于java - Android AssetServer 扩展 NanoHTTPD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22951672/

相关文章:

java - 密码检查成功后加载新网页

android - Mobile Firemonkey 应用本地化

java - Android 服务 android.os.BinderProxy 错误

javascript - 如何使用 Cordova 在手机中缓存数据?

cordova - ionic / Cordova : Add intent-filter using config. xml

java - JBoss 和 MySQL 新的缺失/未满足的依赖关系

java - 如何理解 fragment ?

java - 从 Python 调用 Java 代码的最佳方式是什么?

android - 捕获 android 应用程序 http 流量

android - Sencha 2.4 + cordova 应用程序无法在 Android 上加载 "TypeError: Result of expression ' b.onTargetTouchMove.bind' [undefined] 不是函数。在...”