java手机归属地查询自用方法

本文介绍了一种使用京东API查询手机号码归属地的方法,通过发送GET请求至指定URL,解析返回的GBK格式数据,实现了手机号码归属地的查询功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个查询手机归属地用的是京东的连接,但是返回的是GBK格式,会出现乱码,所以写了一个方法,

使用的URL是https://chongzhi.jd.com/json/order/search_searchPhone.action?mobile=1887*******;

    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;

    @ApiOperation("获取手机号码归属地")
    @SysLog("获取手机号码归属地")
    @Grant
    @GetMapping("/search/{mobile}")
    public R search(@PathVariable("mobile")String mobile){
        String url = "https://chongzhi.jd.com/json/order/search_searchPhone.action?mobile=" + mobile;
        String s1 = null;
        try {
            HttpResponse response = HttpClients.createDefault().execute(
                    new HttpGet(url));
            if (response.getStatusLine().getStatusCode() == 200) {
                s1 = EntityUtils.toString(response.getEntity());
            }
        } catch (Exception e) {
            logger.error("获取手机号码归属地异常", e);
            e.printStackTrace();
        }
        SearchMobileModel searchMobileModel = JSONObject.parseObject(s1, SearchMobileModel.class);
        return R.ok().put("result", searchMobileModel);
    }

用了实体类

import io.swagger.annotations.ApiModel;
import lombok.Data;

import java.io.Serializable;


@Data
@ApiModel("手机号码归属地")
public class SearchMobileModel implements Serializable {
     private Integer area;
     private String areaName;
     private String providerName;
     private Integer provider;
}

代码中的方法可以直接用,导的包在上面,仅仅是自己做个记录,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值