public T HttpGet <T>() { T result = default(T); string webResponse = string.Empty; try { if ((int)Android.OS.Build.VERSION.SdkInt > 9) { Android.OS.StrictMode.ThreadPolicy policy = new Android.OS.StrictMode.ThreadPolicy.Builder().PermitAll().Build(); Android.OS.StrictMode.SetThreadPolicy(policy); } using (var httpclient = new DefaultHttpClient()) { using (var request = new HttpGet()) { request.URI = new Java.Net.URI(Url + GetParams()); using (var response = httpclient.Execute(request)) { webResponse = StreamToString(response.Entity.Content).Replace("https:", "http:").ToString(); } } } } catch (Exception e) { webResponse = string.Empty; } if (string.IsNullOrEmpty(webResponse)) { webResponse = "{}"; } result = Newtonsoft.Json.JsonConvert.DeserializeObject <T>(webResponse); return(result); }
protected override void OnCreate(Android.OS.Bundle bundle) { #if MONODROID_TIMING Log.Info("MonoDroid-Timing", "HelloActivity.OnCreate: time: {0}", (DateTime.Now - new DateTime(1970, 1, 1)).TotalMilliseconds); #endif base.OnCreate(bundle); Console.WriteLine("this is my\nstdout\nmessage! yay!"); Console.Error.WriteLine("this is my\nstderr\nmessage! yay!"); #if __ANDROID_11__ // Android 11+ _really_ doesn't want you to do networking from the main thread: // http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html // I want to do so, so... var policy = new Android.OS.StrictMode.ThreadPolicy.Builder() .PermitAll() .Build(); Android.OS.StrictMode.SetThreadPolicy(policy); #endif if (typeof(OnCreateApp) != ApplicationContext.GetType()) { throw new InvalidOperationException("Wrong Application type created!"); } if (typeof(OnCreateApp) != Application.Context.GetType()) { throw new InvalidOperationException("Wrong Application.Context type created!"); } if (!object.ReferenceEquals(Application.Context, ApplicationContext)) { throw new InvalidOperationException("Wrong Application.Context instance created!"); } var ignore = new HelloActivity(); var ignore2 = new CompanyAdapter(this, 0, new string [0]); RequestWindowFeature(WindowFeatures.NoTitle); StartService(new Intent(this, typeof(MyService))); textview = new TextView(this); textview.Text = "Hello MonoDroid. Mono loves you.\n\nEmbedded\u0000Nulls"; #region BNC_635129 textview.Tag = "2"; if (Convert.ToString(textview.Tag) != "2") { throw new InvalidOperationException("Tag(\"2\") != \"2\"!"); } #endregion TestDllImport(textview); #region BNC_679599 bar = new B <A> (); bar.Do(); #endregion var tempfile = Path.GetTempFileName(); textview.Text += "\n\nPath.GetTempFileName()=" + tempfile; File.Delete(tempfile); int[] style = global::Mono.Samples.SanityTests.Resource.Styleable.NavBar; textview.Text += "\n\nEncoding.GetEncoding(1252).EncodingName=" + Encoding.GetEncoding(1252).EncodingName; TestGzip(); TestArrays(); TestTypeConversion(); TestJniInvocation(textview); TestManualOverrides(); TestBnc631336(); TestBnc632470(); TestBnc654527(textview); var db = ItemsDb.Instance; TestSqlite(textview); TestProxyInstantiation(); TestHttps(textview); TestExceptions(textview); TestJavaManagedJavaManagedInvocation(); TestInputStream(textview); TestOutputStream(textview); TestStreamInvokers(textview); TestNonJavaObjectGenerics(textview); TestUrlConnectionStream(textview); TestConvert(textview); TestTimeZoneInfo(textview); TestAssets(textview); TestUdpSocket(textview); TestNonStaticNestedType(textview); TestFinalization(textview); #if MAPS TestMaps(textview); #endif TestNumerics(textview); TestManagedToJniLookup_Release(textview); var die = new TextView(this) { Text = "Added!", }; WindowManager.AddView(die, new WindowManagerLayoutParams()); WindowManager.RemoveView(die); var scrollView = new ScrollView(this); scrollView.AddView(textview); SetContentView(scrollView); textview.Touch += OnTouch; ThreadPool.QueueUserWorkItem(o => { Log.Info("*jonp*", "Hello from the thread pool thread!"); RunOnUiThread(() => textview.Text += "\n\nThreadPool update!"); }); GC.Collect(); }
public static IReadOnlyList <HostName> GetHostNames() { Android.OS.StrictMode.ThreadPolicy prevPolicy = null; if (Android.OS.Build.VERSION.SdkInt > Android.OS.BuildVersionCodes.Gingerbread) { // required to access interfaceAddress.Address.CanonicalHostName (below), without creating new task, outside UI thread prevPolicy = Android.OS.StrictMode.GetThreadPolicy(); var policy = new Android.OS.StrictMode.ThreadPolicy.Builder().PermitAll().Build(); Android.OS.StrictMode.SetThreadPolicy(policy); } var uwpList = new List <HostName>(); var netInterfacesEnum = Java.Net.NetworkInterface.NetworkInterfaces; while (netInterfacesEnum.HasMoreElements) { var netInterface = netInterfacesEnum.NextElement() as Java.Net.NetworkInterface; if (netInterface.InterfaceAddresses != null) { string androDisplayName = netInterface.DisplayName; // another name, netInterface.Name, would be ignored (seems like == androDisplayName) foreach (var interfaceAddress in netInterface.InterfaceAddresses) { int androPrefixLength = interfaceAddress.NetworkPrefixLength; if (interfaceAddress.Address != null) { string androCanonical = interfaceAddress.Address.CanonicalHostName; // seems like == androCanonical string androHostName = interfaceAddress.Address.HostName; bool androIPv46 = (interfaceAddress.Address.GetAddress().Count() == 4); // we have all required data from Android, and we can use them HostName newHost = new HostName(); IPInformation newInfo = new IPInformation(); newInfo.PrefixLength = (byte)androPrefixLength; newHost.IPInformation = newInfo; // only these two types; UWP has also 'DomainName' and 'Bluetooth' newHost.Type = (androIPv46) ? HostNameType.Ipv4 : HostNameType.Ipv6; newHost.CanonicalName = androCanonical; newHost.RawName = androHostName; newHost.DisplayName = androDisplayName; // assuming there would be no duplicates uwpList.Add(newHost); } } } } if (Android.OS.Build.VERSION.SdkInt > Android.OS.BuildVersionCodes.Gingerbread) { // returning old policy Android.OS.StrictMode.SetThreadPolicy(prevPolicy); } return(uwpList); }