@@ -29,6 +29,13 @@ import com.xuexiang.xutil.XUtil
2929import com.xuexiang.xutil.app.IntentUtils
3030import com.xuexiang.xutil.data.DateUtils
3131import com.xuexiang.xutil.resource.ResUtils.getString
32+ import kotlinx.coroutines.CoroutineScope
33+ import kotlinx.coroutines.Dispatchers
34+ import kotlinx.coroutines.launch
35+ import kotlinx.coroutines.runBlocking
36+ import okhttp3.OkHttpClient
37+ import okhttp3.Request
38+ import org.json.JSONObject
3239import java.text.SimpleDateFormat
3340import java.util.Locale
3441import kotlin.math.min
@@ -397,6 +404,41 @@ class PhoneUtils private constructor() {
397404 return contactInfoList
398405 }
399406
407+ // 获取号码归属地
408+ fun getPhoneArea (phoneNumber : String ): String {
409+ val client = OkHttpClient ()
410+ val url = " https://cx.shouji.360.cn/phonearea.php?number=$phoneNumber "
411+ val request = Request .Builder ().url(url).build()
412+
413+ var result = getString(R .string.unknown_area) // 默认值
414+
415+ // 使用协程来执行网络请求
416+ runBlocking {
417+ val job = CoroutineScope (Dispatchers .IO ).launch {
418+ try {
419+ val response = client.newCall(request).execute()
420+ if (response.isSuccessful) {
421+ val responseData = response.body()?.string()
422+ Log .i(TAG , " getPhoneArea: $responseData " )
423+ if (responseData != null ) {
424+ val jsonObject = JSONObject (responseData)
425+ val data = jsonObject.getJSONObject(" data" )
426+ val province = data.getString(" province" )
427+ val city = data.getString(" city" )
428+ val sp = data.getString(" sp" )
429+ result = " $province $city $sp "
430+ }
431+ }
432+ } catch (e: Exception ) {
433+ e.printStackTrace()
434+ }
435+ }
436+ job.join() // 等待协程执行完毕
437+ }
438+
439+ return result
440+ }
441+
400442 // 获取联系人姓名
401443 fun getContactByNumber (phoneNumber : String? ): MutableList <ContactInfo > {
402444 val contactInfoList = mutableListOf<ContactInfo >()
0 commit comments