Java Android AssetServer扩展NanoHTTPD

Java Android AssetServer扩展NanoHTTPD,java,android,cordova,nanohttpd,Java,Android,Cordova,Nanohttpd,我正在开发一个扩展NanoHTTPD的AssetServer,以便访问基于文件的页面无法使用的Javascript功能 这是我目前掌握的代码: class AssetServer extends NanoHTTPD{ private Activity activity; public AssetServer(int port, Activity activity) { super(port); this.activity = activ

我正在开发一个扩展NanoHTTPD的AssetServer,以便访问基于文件的页面无法使用的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
04-08 19:19:54.548:I/CordovaLog(16411):找到起始页位置:index.html
04-08 19:19:54.553:D/白名单(16411):对网络资源的无限制访问
04-08 19:19:54.553:D/CordovaActivity(16411):恢复应用程序
04-08 19:19:54.553:D/CordovaActivity(16411):CB-3064:错误URL为空
04-08 19:19:54.568:D/dalvikvm(16411):GC_并发释放252K,17%释放7664K/9156K,暂停4ms+9ms,总计33ms
04-08 19:19:54.578:D/webcore(16411):核心加载URL:已调用
04-08 19:19:54.578:D/webkit(16411):防火墙不为空
04-08 19:19:54.578:D/webkit(16411):euler:isUrlBlocked=false
04-08 19:19:54.588:D/软键盘检测(16411):忽略此事件
04-08 19:19:54.673:D/libEGL(16411):loaded/system/lib/egl/libEGL_.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:服务于www/index.html
04-08 19:19:54.693:D/libEGL(16411):loaded/system/lib/egl/libGLESv2_.so
04-08 19:19:54.698:E/(16411):设备驱动程序API匹配
04-08 19:19:54.698:E/(16411):设备驱动程序API版本:20
04-08 19:19:54.698:E/(16411):用户空间API版本:20
04-08 19:19:54.698:E/(16411):马里:修订版=Linux-r3p2-01rel2构建日期=2013年9月2日星期一14:16:28
04-08 19:19:54.733:D/OpenGLRenderer(16411):启用调试模式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:服务于www/lib/jquery-2.0.2.min.js
04-08 19:19:54.833:D/dalvikvm(16411):GC_并发释放100K,16%释放7978K/9404K,暂停8ms+3ms,总计35ms
04-08 19:19:54.863:D/软键盘检测(16411):忽略此事件
04-08 19:19:55.198:I/GATE(16411):开发行动完成
04-08 19:19:55.198:D/CordovaWebViewClient(16411):第页完成(http://localhost:16086/index.html)
2004-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(旋转器,停止)
04-08 19:19:57.243:D/TileManager(16411):启动TG#0,0x539b0050
04-08 19:19:57.243:D/TileManager(16411):来自框架的新EGLContext:52aeba78
04-08 19:19:57.243:D/GLWebViewState(16411):重新设置着色器
04-08 19:19:57.283:D/GLWebViewState(16411):重新输入传输队列
提供的文件并不总是相同的,这表明存在并发问题

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

非常适合这种情况

@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);
}
@覆盖
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init();
//在config.xml中设置
最终活动=此;
HttpServer服务器=新的HttpServer();
addRequestHandler(新的HttpRequestHandler(){
@凌驾
公共HttpResponse HandlerRequest(HttpRequest请求){
InputStream=null;
String path=“www”+request.getUri();
字符串响应=null;
试一试{
is=activity.getAssets().open(路径);
int size=is.available();
字节[]缓冲区=新字节[大小];
is.read(缓冲区);
is.close();
响应=新字符串(缓冲区);
}捕获(ioe异常ioe){
ioe.printStackTrace();
}
System.out.println(“embedhttp:service:“+path+”,“+response.length()+“B”);
返回新的HttpResponse(HttpStatus.OK,response);
}
});
试一试{
服务器绑定(16086);
}捕获(ioe异常ioe){
ioe.printStackTrace();
}
server.start();
//super.loadUrl(Config.getStartUrl());
super.loadUrl(“http://localhost:16086/index.html", 5000);
}

我验证了我确实从资产中获得了一个字符串。