使用mob提供的接口 查询IP对应的省市区信息(json对象转java对象)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用mob提供的接口 查询IP对应的省市区信息(json对象转java对象)相关的知识,希望对你有一定的参考价值。

登录地址:http://api.mob.com

1、写一个发送请求的工具类

2、定义一个接受返回值的类

3、发送请求

---------这里是发送请求的工具类------------------

package com.haochedai.util;

import com.sun.org.apache.xml.internal.utils.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.jboss.netty.handler.codec.http.HttpHeaders.Values.CHARSET;

/**
* Author:wwei
* DESC:HttpClient工具类
*/
public class HttpClientUtil {


private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

public static String doGet(String url) {
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
String tempTitle = null;
try {
HttpGet httpGet = new HttpGet(url);
// 执行get请求
HttpResponse httpResponse;
httpResponse = closeableHttpClient.execute(httpGet);
// 获取响应消息实体
HttpEntity entity = httpResponse.getEntity();
// 判断响应实体是否为空
if (entity != null) {
return EntityUtils.toString(entity, "UTF-8");
} else {
return null;
}
} catch (Exception e) {
logger.error(tempTitle);
logger.error(e.toString());
} finally {
try {
// 关闭流并释放资源
closeableHttpClient.close();
} catch (IOException e) {
logger.error(e.toString());
}
}
return null;
}


public static String doGet(String url, Map<String, Object> params) {
if(!StringUtils.hasText(url)){
return "";
}
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
try {
if (params != null && !params.isEmpty()) {
List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
for (String key : params.keySet()) {
pairs.add(new BasicNameValuePair(key, params.get(key).toString()));
}
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, "UTF-8"));
}
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = closeableHttpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
// 判断响应实体是否为空
if (entity != null) {
return EntityUtils.toString(entity, "UTF-8");
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 关闭流并释放资源
closeableHttpClient.close();
} catch (IOException e) {
logger.error(e.toString());
}
}
return null;
}


public static String doPost(RequestObject object) {
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
String tempTitle = null;
try {
HttpPost httpPost = new HttpPost(object.toUrl());
// 执行get请求
HttpResponse httpResponse;
httpResponse = closeableHttpClient.execute(httpPost);
// 获取响应消息实体
HttpEntity entity = httpResponse.getEntity();
// 判断响应实体是否为空
if (entity != null) {
return EntityUtils.toString(entity, "UTF-8");
} else {
return null;
}
} catch (Exception e) {
logger.error(tempTitle);
logger.error(e.toString());
} finally {
try {
// 关闭流并释放资源
closeableHttpClient.close();
} catch (IOException e) {
logger.error(e.toString());
}
}
return null;
}


}

---------------------------------------

----------这里定义一个对象,接收返回值----------

package com.haochedai.bean;

/**
* Created by Administrator on 2017/5/26 0026.
*/
public class AreaBean {
private String city;
private String province;

public AreaBean() {
}

public AreaBean(String city, String province) {
this.city = city;
this.province = province;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getProvince() {
return province;
}

public void setProvince(String province) {
this.province = province;
}

}
-------------------------------
--------这里开始使用-----------

package com.haochedai.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Preconditions;
import com.haochedai.bean.AreaBean;
import com.haochedai.exception.PcsRunTimeException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.http.client.HttpClient;
import org.slf4j.LoggerFactory;

/**
* Created by Administrator on 2017/5/26 0026.
* see: http://api.mob.com
*/
public class IpUtil {
static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(IpUtil.class);

private static final String URL = "http://apicloud.mob.com/ip/query?key=这里需要一个key值,需要自己去注册";


public static AreaBean getArea(String ip) {
if (StringUtils.isBlank(ip)) {
return new AreaBean();
}
JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doGet(URL+"&ip="+ip));
String code = jsonObject.getString("retCode");
if (code.equals("200")) {
return jsonObject.getObject("result", AreaBean.class);
} else {
String msg = jsonObject.getString("msg");
LOGGER.error("IP地址解析异常: errorCode={}, msg={}", code, msg);
throw new PcsRunTimeException(msg);
}
}
//    public static void main(String[] args) {
// System.out.println(IpUtil.getArea("112.27.205.106").getCity()+","+IpUtil.getArea("112.27.205.106").getProvince());
// }

}

 
























































































































































































































以上是关于使用mob提供的接口 查询IP对应的省市区信息(json对象转java对象)的主要内容,如果未能解决你的问题,请参考以下文章

几个免费IP地址查询接口(API)

分享几个免费IP地址查询接口(API)

分享几个免费IP地址查询接口(API)

使用Python的http.server实现一个简易的Web Api对外提供HanLP拼音转换服务

全网最全的免费api接口-IP地址/域名归属地查询

13 使用Bind提供域名解析服务