Login Address: http://api.mob.com
1. Write a tool class for sending requests
2. Define a class that accepts return values
3. Send Request
---------Here is the tool class for sending requests------------------
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 Tool Class
*/
public class Httpclientutil {
private static final Logger Logger = Loggerfactory.getlogger (Httpclientutil.class);
public static string doget (string url) {
Create Httpclientbuilder
Httpclientbuilder Httpclientbuilder = Httpclientbuilder.create ();
Closeablehttpclient closeablehttpclient = Httpclientbuilder.build ();
String temptitle = null;
try {
HttpGet httpget = new HttpGet (URL);
Perform a GET request
HttpResponse HttpResponse;
HttpResponse = Closeablehttpclient.execute (HttpGet);
Get Response message Entity
httpentity entity = httpresponse.getentity ();
Determine if the response entity is empty
if (Entity! = null) {
Return entityutils.tostring (Entity, "UTF-8");
} else {
return null;
}
} catch (Exception e) {
Logger.error (Temptitle);
Logger.error (E.tostring ());
} finally {
try {
Close a stream and release resources
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 ();
Determine if the response entity is empty
if (Entity! = null) {
Return entityutils.tostring (Entity, "UTF-8");
} else {
return null;
}
} catch (Exception e) {
E.printstacktrace ();
} finally {
try {
Close a stream and release resources
Closeablehttpclient.close ();
} catch (IOException e) {
Logger.error (E.tostring ());
}
}
return null;
}
public static String DoPost (Requestobject object) {
Create Httpclientbuilder
Httpclientbuilder Httpclientbuilder = Httpclientbuilder.create ();
Closeablehttpclient closeablehttpclient = Httpclientbuilder.build ();
String temptitle = null;
try {
HttpPost HttpPost = new HttpPost (Object.tourl ());
Perform a GET request
HttpResponse HttpResponse;
HttpResponse = Closeablehttpclient.execute (HttpPost);
Get Response message Entity
httpentity entity = httpresponse.getentity ();
Determine if the response entity is empty
if (Entity! = null) {
Return entityutils.tostring (Entity, "UTF-8");
} else {
return null;
}
} catch (Exception e) {
Logger.error (Temptitle);
Logger.error (E.tostring ());
} finally {
try {
Close a stream and release resources
Closeablehttpclient.close ();
} catch (IOException e) {
Logger.error (E.tostring ());
}
}
return null;
}
}
---------------------------------------
----------define an object here that receives the return value----------
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;
}
}
-------------------------------
--------here to start using-----------
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= need a key value, you need to register";
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 Address Resolution exception: 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 ());
// }
}
Use the interface provided by mob to query the IP corresponding to the provincial and municipal information (JSON object to Java object)