首页 > 代码库 > 使用mob提供的接口 查询IP对应的省市区信息(json对象转java对象)
使用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对象)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。