2014-04-09 4 views
1

file : // 기반 페이지에서 사용할 수없는 Javascript 기능에 액세스하기 위해 NanoHTTPD를 확장하는 AssetServer를 개발 중입니다.Android AssetServer extends NanoHTTPD

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; 

    } 

} 

내 휴대 전화에서 안드로이드 응용 프로그램을 실행되는 로그 캣 출력 페이지 일부를 수신 제안 전부는 아니지만, 파일 페이지에서 요청 :

내가 지금까지 가지고있는 코드입니다. 서버에 제공하기 전에 워밍업 시간을주기 위해 loadUrl 호출에 5 초의 지연이 있습니다. 그것은 항상 제공하는 동일한 파일이 아니다

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 

, 동시성 문제를 제안 :

다음은 로그 캣 출력입니다.

누구나 내가 뭘 잘못하고 있는지 알아? 미리 감사드립니다.

+0

나는 실제로 자산에서 문자열을 얻고 있음을 확인했습니다. – lowerkey

답변

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