我正在开发一个扩展 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 调用有 5 秒的延迟,以便在提供页面之前给服务器一些时间来预热。
这是 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
提供的文件并不总是相同,这表明存在并发问题。
有人知道我在做什么错吗?提前致谢!