2014-01-22 20 views
3

我在寫一個需要脫機訪問的移動應用程序(理想情況下,無需連接到外部網絡服務器)。該應用程序正在使用AngularJS框架進行模板/數據綁定。在線服務脫機html 5在Android上的移動應用程序

應用程序服務和數據層是用Xamarin編寫的。用戶界面將最理想的是採用被寫入HTML5/angularJs

角度不使用文件工作:///的URL,我調查了以下選項:

  1. 離線應用(服務文件從網絡服務器的外部) a。使用cache.manifest來確保應用程序脫機工作。 b。 Javascript橋樑從數據層獲取信息。

  2. 將Web服務器作爲應用程序的一部分來連接用戶界面和服務。

我非常希望去與解決方案2 - 因爲這意味着應用程序可以在完全脫機模式下工作。

在Android應用程序中運行Web服務器有任何問題嗎?任何人都可以推薦一個.Net網絡服務器,可以合併在Xamarin中 - 或者可以集成的android .jar網絡服務器?

+0

我感興趣的選項之一,你做的任何進展? – vtortola

回答

0

您可以使用本地的file:// URL直接在WebView上打開HTML和Javascript文件。你不需要Web服務器。

只需添加一個WebViewload a local file

<?xml version="1.0" encoding="utf-8"?> 
<WebView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/LocalWebView"> 
</WebView> 
using Android.Webkit; 

SetContentView (Resource.Layout.Main); 

WebView localWebView = FindViewById<WebView>(Resource.Id.LocalWebView); 

localWebView.LoadUrl("file:///android_asset/Content/Home.html"); 
+0

這正是我正在做的,它適用於標準的html/js應用程序。然而,anularJS對網址做了一些時髦的事情......例如當它加載第一個視圖時,它將URL從file:///android_asset/Content/Home.html更改爲file:///android_asset/Content/Home.html#/ –

+0

您是否嘗試了默認的.NET Web服務器容器?自己託管的HTTP服務器?足夠了嗎? –

+0

這將有希望完成這項工作...我不知道.net有一個自我託管的網絡服務器...我會試一試,看看它是否有效...會讓你張貼。 –

0

您可以使用自託管服務器,並使用它與網頁視圖頁面。

您可以使用NOWIN project自行託管。下載here

下面是一個簡單的自我託管的Web服務器響應端口8080上的一個樣本:

using System; 
using System.IO; 
using System.Threading.Tasks; 
using Microsoft.Owin.Hosting; 
using Owin; 

namespace OwinHostingSample 
{ 
    static class Program 
    { 
     static void Main(string[] args) 
     { 
      var options = new StartOptions 
      { 
       ServerFactory = "Nowin", 
       Port = 8080 
      }; 

      using (WebApp.Start<Startup>(options)) 
      { 
       Console.WriteLine("Running a http server on port 8080"); 
       Console.ReadKey(); 
      } 
     } 
    } 

    public class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      app.Run(c => 
      { 
       var path = c.Request.Path.Value; 
       if (path == "/") 
       { 
        c.Response.StatusCode = 200; 
        c.Response.ContentType = "text/plain"; 
        c.Response.Write("Hello World!"); 
        return Task.Delay(0); 
       } 
       if (path.Contains("..")) 
       { 
        // hackers .. 
        c.Response.StatusCode = 500; 
        return Task.Delay(0); 
       } 
       if (File.Exists(path.Substring(1))) 
       { 
        c.Response.StatusCode = 200; 
        c.Response.ContentType = path.EndsWith(".js") ? "application/javascript" : "text/html"; 
        return c.Response.WriteAsync(File.ReadAllBytes(path.Substring(1))); 
       } 
       c.Response.StatusCode = 404; 
       return Task.Delay(0); 
      }); 
     } 
    } 
} 
相關問題