Android AssetServer расширяет NanoHTTPD
Я разрабатываю AssetServer, расширяющий NanoHTTPD для доступа к функциям Javascript, недоступным для страниц на основе file://.
Это код, который я до сих пор:
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
Это не всегда те же файлы, которые обслуживаются, предлагая проблему параллелизма.
Кто-нибудь знает, что я делаю не так? Заранее спасибо!
1 ответ
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);
}