From acc8caee31c4be90bd38d1af18136b0e84f6fe94 Mon Sep 17 00:00:00 2001 From: wxr <wxr@hdlchina.com.cn> Date: 星期五, 27 九月 2024 14:01:19 +0800 Subject: [PATCH] Merge branch 'feature/v2.7_迭代' into feature/V2.7_Google --- HDL-ON_Android/Other/JLCountrycode.cs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 40 insertions(+), 0 deletions(-) diff --git a/HDL-ON_Android/Other/JLCountrycode.cs b/HDL-ON_Android/Other/JLCountrycode.cs index 487892b..6590e00 100644 --- a/HDL-ON_Android/Other/JLCountrycode.cs +++ b/HDL-ON_Android/Other/JLCountrycode.cs @@ -4,6 +4,9 @@ using Com.Widget.Jlcountrycode.Sortselect; using System.Collections.Generic; using Java.Util; +using Newtonsoft.Json.Linq; +using System.Threading.Tasks; +using System.Net.Http; namespace JLCountrycode { @@ -27,6 +30,41 @@ return m_Current; } } + + + /// <summary> + /// 鏍规嵁鎵嬫満褰撳墠鐨処P鑾峰彇鍥藉淇℃伅 + /// </summary> + /// <returns></returns> + public async Task<string> GetCountryByIP() + { + string country = "Unknown"; + + try + { + using (HttpClient client = new HttpClient()) + { + // 浣跨敤 ipinfo.io 鑾峰彇浣嶇疆淇℃伅 + HttpResponseMessage response = await client.GetAsync("https://ipinfo.io/json"); + + if (response.IsSuccessStatusCode) + { + string json = await response.Content.ReadAsStringAsync(); + JObject jsonObject = JObject.Parse(json); + + // 浠庤繑鍥炵殑 JSON 涓彁鍙栧浗瀹朵俊鎭� + country = jsonObject["country"].ToString(); + } + } + } + catch (Exception ex) + { + Console.WriteLine($"Error fetching IP info: {ex.Message}"); + } + + return country; + } + /// <summary> /// 鍥藉鍖哄彿閫夋嫨 @@ -125,4 +163,6 @@ } + + } \ No newline at end of file -- Gitblit v1.8.0