Java 查询手机号归属地 基于淘宝Api,百度Api
2020-07-27 本文已影响0人
橘子软件
代码基于hutool工具包 和 lombok制作
主要代码是用hutool提供http工具类和json工具类进行完成
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.9</version>
</dependency>
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class PhoneEntity {
private String province;
private String city;
// 如 中国联通...
private String type;
}
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.szxcharge.cloud.user.common.entity.PhoneEntity;
import lombok.extern.slf4j.Slf4j;
/**
* 手机号查询省份 运营商信息
* @author orange
* @email 1067357662@qq.com
*/
@Slf4j
public class PhoneQueryUtil {
/**
* 查询手机号信息
* @return null=信息查询不到
*/
public static PhoneEntity query(String phoneNumber) {
PhoneEntity phoneEntity = queryByBaiduApi(phoneNumber);
if (null == phoneEntity) {
phoneEntity = queryByTaobaoApi(phoneNumber);
}
return phoneEntity;
}
/**
* 通过淘宝接口查询手机号信息
*/
private static PhoneEntity queryByTaobaoApi(String phoneNumber) {
try {
String url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=" + phoneNumber;
HttpResponse response = HttpRequest.get(url).execute();
if (response.getStatus() != HttpStatus.HTTP_OK) return null;
String body = response.body().replace("__GetZoneResult_ = ", "");
if (StrUtil.isBlank(body)) return null;
JSONObject parseObj = JSONUtil.parseObj(body);
String province = parseObj.getStr("province");
String type = parseObj.getStr("catName");
if (StrUtil.isBlank(province) || StrUtil.isBlank(type)) return null;
PhoneEntity phoneEntity = new PhoneEntity();
phoneEntity.setProvince(province);
phoneEntity.setType(type);
return phoneEntity;
} catch (Exception e) {
log.error("手机号查询信息出错: {}", ExceptionUtil.stacktraceToString(e));
return null;
}
}
/**
* 通过百度接口查询手机号信息
*/
private static PhoneEntity queryByBaiduApi(String phoneNumber) {
try {
String url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?resource_name=guishudi&query=" + phoneNumber;
HttpResponse response = HttpRequest.get(url).execute();
if (response.getStatus() != HttpStatus.HTTP_OK) return null;
String body = response.body();
if (StrUtil.isBlank(body)) return null;
JSONObject parseObj = JSONUtil.parseObj(body);
JSONArray parseObjJSONArray = parseObj.getJSONArray("data");
if (null == parseObjJSONArray || parseObjJSONArray.size() == 0) return null;
JSONObject dataObject = parseObjJSONArray.getJSONObject(0);
String province = dataObject.getStr("prov");
String type = dataObject.getStr("type");
String city = dataObject.getStr("city");
if (StrUtil.isBlank(province) || StrUtil.isBlank(type)) return null;
PhoneEntity phoneEntity = new PhoneEntity();
phoneEntity.setProvince(province);
phoneEntity.setType(type);
phoneEntity.setCity(city);
return phoneEntity;
} catch (Exception e) {
log.error("手机号查询信息出错: {}", ExceptionUtil.stacktraceToString(e));
return null;
}
}
public static void main(String[] args) {
PhoneEntity phoneEntity = PhoneQueryUtil.query("xxxxxxxxx");
if (null != phoneEntity) {
System.out.println(phoneEntity.getProvince());
System.out.println(phoneEntity.getType());
System.out.println(phoneEntity.getCity());
}
}
}