//第一步 权限:
<uses-permission android:name="android.permission.INTERNET"/>
//第二步 在Activity里使用:
private void serchPhone(final String s) {
new Thread() {
public void run() {
String path = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=" + s;
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 提交模式
connection.setRequestMethod("GET");
//读取超时 单位毫秒
connection.setReadTimeout(5000);
//连接超时 单位毫秒
connection.setConnectTimeout(5000);
//获取
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
InputStream inputStream = connection.getInputStream();
String string = streamToString(inputStream, "gbk");
final String json = string.substring(string.indexOf("{"));
final Message message = Message.obtain();
message.what = 0;
message.obj = json;
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.e("TAG1", "" + json);
try {
JSONObject jsonObject = new JSONObject(json);
String catName = jsonObject.getString("catName");
String telString = jsonObject.getString("telString");
String carrier = jsonObject.getString("carrier");
Toast.makeText(MainActivity.this, "" + catName, Toast.LENGTH_SHORT).show();
Log.e("TAG2", "" + catName);
Log.e("TAG3", "" + telString);
Log.e("TAG4", "" + carrier);
mText.setText(catName+","+telString+","+carrier);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Log.d("string", json);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
;
}.start();
}
private String streamToString(InputStream inputStream, String charset) {
try {
//输入流
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, charset);
//得到缓冲流
BufferedReader reader = new BufferedReader(inputStreamReader);
String s = null;
StringBuilder builder = new StringBuilder();
while ((s = reader.readLine()) != null) {
builder.append(s);
}
reader.close();
return builder.toString();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//然后在点击事件里调用:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mButton:
serchPhone("13777778888");
break;
}
}
//我的Activity布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.hasee.a1022ceshi2.MainActivity">
<Button
android:gravity="center"
android:id="@+id/mButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打电话"
/>
<TextView
android:id="@+id/mText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
//okhttp请求方式:
private void OkHttp() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=" + "手机号")
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
String string = response.body().string();
final String json = string.substring(string.indexOf("{"));
JSONObject jsonObject = new JSONObject(json);
String catName = jsonObject.getString("catName");
String telString = jsonObject.getString("telString");
String carrier = jsonObject.getString("carrier");
Log.e("TAG1", "" + json);
Log.e("TAG2", "" + catName);
Log.e("TAG3", "" + telString);
Log.e("TAG4", "" + carrier);
Toast.makeText(MainActivity.this, "" + catName, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
}
通过google归属地数据库查询:
//手机号查询库归属地、运营商等信息 implementation 'com.googlecode.libphonenumber:libphonenumber:8.12.8' implementation 'com.googlecode.libphonenumber:carrier:1.134' implementation 'com.googlecode.libphonenumber:geocoder:2.144'
/** * 通过google电话号码信息库查询归属地运营商等信息 * * @author CJF */ public class PhoneNumberInfoUtil { private final String TAG ="PhoneNumberInfoUtil"; private volatile static PhoneNumberInfoUtil phoneNumberInfoUtil = null; /** * 默认国家代码 中国 * COUNTRY_CODE = phoneNumberUtil.getCountryCodeForRegion(Locale.getDefault().getCountry()); */ private final Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber().setCountryCode(86); /** * 获取手机归属地初始化 */ private final PhoneNumberOfflineGeocoder geoInstance = PhoneNumberOfflineGeocoder.getInstance(); /** * 获取手机号运营商初始化 */ private final PhoneNumberToCarrierMapper carrierInstance = PhoneNumberToCarrierMapper.getInstance(); /** * 判断手机号是否有效初始化 */ private final PhoneNumberUtil instance = PhoneNumberUtil.getInstance(); public static PhoneNumberInfoUtil getInstance() { if (null == phoneNumberInfoUtil) { synchronized (PhoneNumberInfoUtil.class) { if (null == phoneNumberInfoUtil) { phoneNumberInfoUtil = new PhoneNumberInfoUtil(); } } } return phoneNumberInfoUtil; } /** * 根据国家代码和手机号 判断手机号是否有效 * * @param phoneNumber * @return */ public boolean checkPhoneNumber(String phoneNumber) { boolean validNumber = false; try { Phonenumber.PhoneNumber number = pn.setNationalNumber(Long.parseLong(phoneNumber)); validNumber = instance.isValidNumber(number); } catch (Exception e) { e.printStackTrace(); } return validNumber; } /** * 根据国家代码和手机号,获取手机号运营商 * * @param phoneNumber * @return */ public String getCarrier(String phoneNumber) { Log.d(TAG, "phoneNumber:" + phoneNumber); String carrierEn = ""; try { Phonenumber.PhoneNumber number = pn.setNationalNumber(Long.parseLong(phoneNumber)); String nameForNumber = carrierInstance.getNameForNumber(number, Locale.ENGLISH); carrierEn = TextUtils.isEmpty(nameForNumber) ? "" : nameForNumber; } catch (Exception e) { e.printStackTrace(); } Log.d(TAG, "carrierEn:" + carrierEn); switch (carrierEn) { case "China Mobile": return "移动"; case "China Unicom": return "联通"; case "China Telecom": return "电信"; default: return ""; } } /** * 根据国家代码和手机号,获取手机归属地 * * @param @param phoneNumber * @param @return 归属地 */ public String getGeo(String phoneNumber) { Log.d(TAG, "phoneNumber:" + phoneNumber); String forNumber = "未知归属地"; try { Phonenumber.PhoneNumber number = pn.setNationalNumber(Long.parseLong(phoneNumber)); String descriptionForNumber = geoInstance.getDescriptionForNumber(number, Locale.CHINESE); forNumber = TextUtils.isEmpty(descriptionForNumber) ? forNumber : descriptionForNumber; } catch (Exception e) { e.printStackTrace(); } Log.d(TAG, "phoneNumber:" + phoneNumber); Log.d(TAG, "forNumber:" + forNumber); return forNumber; } }
//——————————————————————–完————————————————————————-
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/118308.html