这个查询手机归属地用的是京东的连接,但是返回的是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;
}
代码中的方法可以直接用,导的包在上面,仅仅是自己做个记录,