wxr
2024-09-23 b2f25bfca6471fe3bfa041da8e4a1d01fd00f6ed
Update JLCountrycode.cs
1个文件已修改
40 ■■■■■ 已修改文件
HDL-ON_Android/Other/JLCountrycode.cs 40 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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>
        /// 根据手机当前的IP获取国家信息
        /// </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 @@
    }
}