Android - Jetty服务器在没有互联网连接的情况下无法正常工作

时间:2015-12-17 15:48:11

标签: android server jetty offline

我在Android上实现了Jetty服务器以打开离线网站。 我使用Jetty 7。

问题是,如果我在没有网络连接的情况下运行服务器并尝试打开本地网站,它就不会运行css文件,就像它没有运行本地网络服务器一样,但如果我打开互联网连接网页它正常运行

网络应用的路径 - http://localhost:1024/storage/emulated/0/offlineWebApp/index.html

本地网络应用 - https://github.com/valnub/Framework7-Pure-Angular-Template

提前致谢。

gradle这个

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile files('libs/gson-2.2.4.jar')

compile files('libs/jetty-continuation-7.3.0.v20110203.jar')
compile files('libs/jetty-http-7.3.0.v20110203.jar')
compile files('libs/jetty-io-7.3.0.v20110203.jar')
compile files('libs/jetty-security-7.3.0.v20110203.jar')
compile files('libs/jetty-server-7.3.0.v20110203.jar')
compile files('libs/jetty-servlet-7.3.0.v20110203.jar')
compile files('libs/jetty-util-7.3.0.v20110203.jar')
compile files('libs/jetty-webapp-7.3.0.v20110203.jar')
compile files('libs/servlet-api-2.5.jar')

}

启动Jetty Server

 public void startOfflineServer(){

    Server server = new Server();
    SelectChannelConnector connector = new SelectChannelConnector();
    connector.setPort(1024);
    server.addConnector(connector);

    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setResourceBase(".");

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[]{resource_handler, new DefaultHandler()});
    server.setHandler(handlers);

    try {
        if(!server.isStarted()){
            server.start();
         }

    } catch (Exception e) {
        e.printStackTrace();
    }

添加离线webView

 public void offlineWebView(){

    //activate hardware accelerator for API 19 and above
    if (Build.VERSION.SDK_INT >= 19) {
        mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    } else {
        mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    webSettings.setDomStorageEnabled(true);
    mWebView.setWebChromeClient(new WebChromeClient());

    //http://localhost:1024/storage/emulated/0/offlineWebApp/index.html

    String externalStorageDirectory = Environment.getExternalStorageDirectory().toString() + "/offlineWebApp/index.html";                                                                                                                                                                                                                                  
    mWebView.loadUrl("http://localhost:1024" + externalStorageDirectory);
}

logcat的

12-17 17:27:46.917 4178-4178/com.scavaline.lms.mobilelms W/dalvikvm: Link of class 'Lorg/eclipse/jetty/util/log/JettyAwareLogger;' failed
12-17 17:27:47.007 4178-4178/com.scavaline.lms.mobilelms W/dalvikvm: dvmFindClassByName rejecting 'org/eclipse/jetty/http/mime_en_US'
12-17 17:27:47.247 4178-4178/com.scavaline.lms.mobilelms W/dalvikvm: dvmFindClassByName rejecting 'org/eclipse/jetty/http/mime_en'
12-17 17:27:47.247 4178-4178/com.scavaline.lms.mobilelms W/dalvikvm: dvmFindClassByName rejecting 'org/eclipse/jetty/http/mime'
12-17 17:27:49.940 4178-4178/com.scavaline.lms.mobilelms W/dalvikvm: dvmFindClassByName rejecting 'org/eclipse/jetty/http/encoding_en_US'
12-17 17:27:49.940 4178-4178/com.scavaline.lms.mobilelms W/dalvikvm: dvmFindClassByName rejecting 'org/eclipse/jetty/http/encoding_en'
12-17 17:27:49.940 4178-4178/com.scavaline.lms.mobilelms W/dalvikvm: dvmFindClassByName rejecting 'org/eclipse/jetty/http/encoding'
12-17 17:27:50.000 4178-4178/com.scavaline.lms.mobilelms W/System.err: 2015-12-17 17:27:50.002:INFO:oejs.Server:jetty-8.0.y.z-SNAPSHOT


12-17 17:28:27.737 4178-4178/? I/chromium: [INFO:CONSOLE(6)] "Viewport argument key "minimal-ui" not recognized and ignored.", source: http://localhost:1024/storage/emulated/0/offlineWebApp/index.html (6)
12-17 17:28:47.856 4178-4178/com.scavaline.lms.mobilelms I/chromium: [INFO:CONSOLE(10)] "Uncaught ReferenceError: angular is not defined", source: http://localhost:1024/storage/emulated/0/offlineWebApp/js/config.js (10)
12-17 17:28:47.866 4178-4178/com.scavaline.lms.mobilelms I/chromium: [INFO:CONSOLE(4)] "Uncaught TypeError: Cannot call method 'factory' of undefined", source: http://localhost:1024/storage/emulated/0/offlineWebApp/js/service/InitService.js (4)
12-17 17:28:47.866 4178-4178/com.scavaline.lms.mobilelms I/chromium: [INFO:CONSOLE(4)] "Uncaught TypeError: Cannot call method 'controller' of undefined", source: http://localhost:1024/storage/emulated/0/offlineWebApp/js/controller/IndexPageController.js (4)
12-17 17:28:47.876 4178-4178/com.scavaline.lms.mobilelms I/chromium: [INFO:CONSOLE(4)] "Uncaught TypeError: Cannot call method 'controller' of undefined", source: http://localhost:1024/storage/emulated/0/offlineWebApp/js/controller/DetailPageController.js 

1 个答案:

答案 0 :(得分:0)

尝试将连接器设置为仅绑定到localhost

connector.setHost("localhost");