Ste*_*ead 5 android webview xamarin
我们有一个xamarin android应用程序,其中包含一个httplistener,它为在全屏webview中运行的单页应用程序提供html,js,css和json调用。从Xamarin 3.5开始,webview无法检索我们在端口31316上运行的本地主机地址。在此发行版之前,此功能正常运行。
从我所看到的情况来看,httplistener看起来很健康,因为我们可以正确地使用WebRequest库来调用它,这使我相信Webview发生了某些变化。
任何帮助将不胜感激。
以下是一个演示行为的示例应用程序:
using System;
using System.Net;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Webkit;
using Android.Widget;
using System.IO;
using System.Text;
namespace HttpListenerTest
{
[Activity (Label = "HttpListenerTest", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
StartService(new Intent(this, typeof(HttpListenerTestService)));
var webView = (WebView)this.FindViewById<WebView>(Resource.Id.webview);
webView.Settings.AllowFileAccess = true;
webView.Settings.BlockNetworkLoads = false;
webView.LoadUrl("http://localhost:31316/");
//webView.LoadData (Activities.html, "text/html", "UTF-8");
var button = FindViewById<Button>(Resource.Id.button1);
button.Click += delegate
{
AlertDialog.Builder alert1;
alert1 = new AlertDialog.Builder(this);
try
{
if(!Activities.httpListener.IsListening)
{
alert1.SetMessage("Listener has stopped listening");
alert1.Show();
return;
}
string url = "http://localhost:31316/" + DateTime.Now.ToString("O");
var request = (HttpWebRequest)WebRequest.Create(new Uri(url));
request.Method = "GET";
string responseString = "";
using (WebResponse response = request.GetResponse())
{
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
responseString = reader.ReadToEnd();
response.Close();
reader.Close();
}
alert1.SetMessage(responseString);
}
catch (Exception e)
{
alert1.SetMessage(e.Message + " " + e.StackTrace);
}
alert1.Show();
};
}
}
[Service(Label = "HttpListenerTest Service")]
public class HttpListenerTestService : Service
{
public override IBinder OnBind(Intent intent)
{
return null;
}
public override void OnCreate()
{
base.OnCreate();
Activities.InitRest();
}
public override void OnDestroy()
{
}
public override void OnStart(Intent intent, int startId)
{
base.OnStart(intent, startId);
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
}
}
public static class Activities
{
public static HttpListener httpListener { get; set; }
public static Thread RestThread { get; set; }
public const string html = "<html>hello world</html>";
public static void InitRest()
{
RestThread = new Thread(StartRest) { Name = "Rest Service" };
RestThread.Start();
}
private static void StartRest()
{
httpListener = new HttpListener { IgnoreWriteExceptions = true };
httpListener.Prefixes.Add(@"http://localhost:31316/");
httpListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
httpListener.Start();
httpListener.BeginGetContext(HandleRequest, httpListener);
}
public static void HandleRequest(IAsyncResult result)
{
Console.WriteLine ("==========================HandleRequest==========================");
var context = httpListener.EndGetContext(result);
var unescapedUrl = Uri.UnescapeDataString(context.Request.RawUrl);
var bytes = new byte[html.Length * sizeof(char)];
Buffer.BlockCopy(html.ToCharArray(), 0, bytes, 0, bytes.Length);
context.Response.ContentLength64 = bytes.Length;
context.Response.OutputStream.Write(bytes, 0, bytes.Length);
context.Response.ContentType = "text/html";
//context.Response.ContentEncoding = "UTF-8";
context.Response.OutputStream.Close();
context.Response.OutputStream.Dispose();
context.Response.StatusCode = 200;
httpListener.BeginGetContext(HandleRequest, httpListener);
}
}
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
568 次 |
| 最近记录: |