zoukankan      html  css  js  c++  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());
    // }

    }

  • 相关阅读:
    Please provide compiled classes of your project with sonar.java.binaries property
    全链路压测
    零宽度短网址生成器
    http://www.easytest.xyz/login_action/
    进程间的五种通信方式介绍
    InteiiJ IDEA中如何制定制定哪一个配置文件
    常见的各种排序算法汇总
    编程面试之前你应该知晓的八大数据结构
    rest-assured-doc接口自动化测试,数据驱动测试平台
    Jenkins 邮件配置 || Jenkins 发送邮件失败,提示:Error sending to the following VALID addresses
  • 原文地址:https://www.cnblogs.com/ph121/p/6909724.html
Copyright © 2011-2022 走看看