Authored by zhaoyue

ADD report location log

@@ -4,10 +4,10 @@ package com.xkl.domain; @@ -4,10 +4,10 @@ package com.xkl.domain;
4 * Created by zhaoyue on 2017/4/15. 4 * Created by zhaoyue on 2017/4/15.
5 */ 5 */
6 public class Location { 6 public class Location {
7 - private String country ;  
8 - private String province ;  
9 - private String city ;  
10 - private String district ; 7 + private String country;
  8 + private String province;
  9 + private String city;
  10 + private String district;
11 11
12 public Location(String country, String province, String city, String district) { 12 public Location(String country, String province, String city, String district) {
13 this.country = country; 13 this.country = country;
@@ -16,6 +16,11 @@ public class Location { @@ -16,6 +16,11 @@ public class Location {
16 this.district = district; 16 this.district = district;
17 } 17 }
18 18
  19 + @Override
  20 + public String toString() {
  21 + return country + province + city + district;
  22 + }
  23 +
19 public String getCountry() { 24 public String getCountry() {
20 return country; 25 return country;
21 } 26 }
@@ -16,46 +16,49 @@ public class HttpTools { @@ -16,46 +16,49 @@ public class HttpTools {
16 16
17 /** 17 /**
18 * 获取当前时间,10位 18 * 获取当前时间,10位
  19 + *
19 * @return 20 * @return
20 */ 21 */
21 - public static String getNow(){ 22 + public static String getNow() {
22 Calendar c = Calendar.getInstance();//可以对每个时间域单独修改 23 Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
23 - return String.valueOf(c.getTimeInMillis()/1000); 24 + return String.valueOf(c.getTimeInMillis() / 1000);
24 } 25 }
25 26
26 27
27 /** 28 /**
28 * 发送http请求 29 * 发送http请求
29 - * @param url 请求url  
30 - * @param type 请求类型:GET,POST,DELETE 30 + *
  31 + * @param url 请求url
  32 + * @param type 请求类型:GET,POST,DELETE
31 * @param params 请求参数:map类型 33 * @param params 请求参数:map类型
32 * @return 34 * @return
33 */ 35 */
34 - public static String requestByMapWithToken(String url, String type, Map<String, String> params, String authorization){  
35 - String reqbody = getRequestData(params,"UTF-8").toString();  
36 - String result = requestByString(url + "?" + reqbody,type,authorization,null); 36 + public static String requestByMapWithToken(String url, String type, Map<String, String> params, String authorization) {
  37 + String reqbody = getRequestData(params, "UTF-8").toString();
  38 + String result = requestByString(url + "?" + reqbody, type, authorization, null);
37 return result; 39 return result;
38 } 40 }
39 41
40 - public static String requestByMap(String url, String type,Map<String, String> params){  
41 - return requestByMapWithToken(url,type,params,""); 42 + public static String requestByMap(String url, String type, Map<String, String> params) {
  43 + return requestByMapWithToken(url, type, params, "");
42 } 44 }
43 45
44 /** 46 /**
45 * 发送http请求 47 * 发送http请求
46 - * @param url 请求url  
47 - * @param type 请求类型:GET,POST,DELETE 48 + *
  49 + * @param url 请求url
  50 + * @param type 请求类型:GET,POST,DELETE
48 * @param reqbody 请求参数:String类型 51 * @param reqbody 请求参数:String类型
49 * @return 52 * @return
50 */ 53 */
51 - public static String requestByString(String url, String type, String authorization,String reqbody){ 54 + public static String requestByString(String url, String type, String authorization, String reqbody) {
52 HttpURLConnection con = null; 55 HttpURLConnection con = null;
53 String result = null; 56 String result = null;
54 try { 57 try {
55 - con = getHttpConnection( url , type, authorization); 58 + con = getHttpConnection(url, type, authorization);
56 //you can add any request body here if you want to post 59 //you can add any request body here if you want to post
57 - if( reqbody != null){  
58 - DataOutputStream out = new DataOutputStream(con.getOutputStream()); 60 + if (reqbody != null) {
  61 + DataOutputStream out = new DataOutputStream(con.getOutputStream());
59 out.writeBytes(reqbody); 62 out.writeBytes(reqbody);
60 out.flush(); 63 out.flush();
61 out.close(); 64 out.close();
@@ -64,7 +67,7 @@ public class HttpTools { @@ -64,7 +67,7 @@ public class HttpTools {
64 BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); 67 BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
65 String temp = null; 68 String temp = null;
66 StringBuilder sb = new StringBuilder(); 69 StringBuilder sb = new StringBuilder();
67 - while((temp = in.readLine()) != null){ 70 + while ((temp = in.readLine()) != null) {
68 sb.append(temp).append(" "); 71 sb.append(temp).append(" ");
69 } 72 }
70 result = sb.toString(); 73 result = sb.toString();
@@ -76,21 +79,21 @@ public class HttpTools { @@ -76,21 +79,21 @@ public class HttpTools {
76 79
77 } 80 }
78 81
79 - private static HttpURLConnection getHttpConnection(String url, String type, String authorization){ 82 + private static HttpURLConnection getHttpConnection(String url, String type, String authorization) {
80 URL uri = null; 83 URL uri = null;
81 HttpURLConnection con = null; 84 HttpURLConnection con = null;
82 - try{ 85 + try {
83 uri = new URL(url); 86 uri = new URL(url);
84 con = (HttpURLConnection) uri.openConnection(); 87 con = (HttpURLConnection) uri.openConnection();
85 con.setRequestMethod(type); //type: POST, PUT, DELETE, GET 88 con.setRequestMethod(type); //type: POST, PUT, DELETE, GET
86 - if(type.equals("POST")) 89 + if (type.equals("POST"))
87 con.setDoOutput(true); 90 con.setDoOutput(true);
88 - con.setConnectTimeout(60000); //60 secs  
89 - con.setReadTimeout(60000); //60 secs 91 + con.setConnectTimeout(2000); //2 secs
  92 + con.setReadTimeout(2000); //2 secs
90 con.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); 93 con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
91 - con.setRequestProperty("authorization",authorization);  
92 - }catch(Exception e){  
93 - System.out.println( "connection i/o failed" ); 94 + con.setRequestProperty("authorization", authorization);
  95 + } catch (Exception e) {
  96 + System.out.println("connection i/o failed");
94 } 97 }
95 98
96 99
@@ -100,7 +103,7 @@ public class HttpTools { @@ -100,7 +103,7 @@ public class HttpTools {
100 private static StringBuffer getRequestData(Map<String, String> params, String encode) { 103 private static StringBuffer getRequestData(Map<String, String> params, String encode) {
101 StringBuffer stringBuffer = new StringBuffer(); //存储封装好的请求体信息 104 StringBuffer stringBuffer = new StringBuffer(); //存储封装好的请求体信息
102 try { 105 try {
103 - for(Map.Entry<String, String> entry : params.entrySet()) { 106 + for (Map.Entry<String, String> entry : params.entrySet()) {
104 stringBuffer.append(entry.getKey()) 107 stringBuffer.append(entry.getKey())
105 .append("=") 108 .append("=")
106 .append(URLEncoder.encode(entry.getValue(), encode)) 109 .append(URLEncoder.encode(entry.getValue(), encode))
@@ -12,14 +12,15 @@ import java.util.Map; @@ -12,14 +12,15 @@ import java.util.Map;
12 * Created by zhaoyue on 2017/4/15. 12 * Created by zhaoyue on 2017/4/15.
13 */ 13 */
14 public class IPTools { 14 public class IPTools {
15 - public static final String sinaIPGetter = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&"; 15 + public static final String sinaIPGetter = "http://int.dpool.sina.com.cn/iplookup/iplookup.php";
16 16
17 public static Location getLocationWithIP(String IP) { 17 public static Location getLocationWithIP(String IP) {
18 try { 18 try {
19 Map<String, String> params = new HashMap<String, String>(); 19 Map<String, String> params = new HashMap<String, String>();
20 - params.put("ip",IP);  
21 - String url = sinaIPGetter + URLEncoder.encode(IP, "utf-8");  
22 - String content = HttpTools.requestByMap(url, "POST", params); 20 + params.put("ip", IP);
  21 + params.put("format", "json");
  22 +
  23 + String content = HttpTools.requestByMap(sinaIPGetter, "POST", params);
23 JSONObject ipJson = JSONObject.parseObject(content); 24 JSONObject ipJson = JSONObject.parseObject(content);
24 String country = ipJson.getString("country"); 25 String country = ipJson.getString("country");
25 String province = ipJson.getString("province"); 26 String province = ipJson.getString("province");
@@ -27,7 +28,7 @@ public class IPTools { @@ -27,7 +28,7 @@ public class IPTools {
27 String district = ipJson.getString("district"); 28 String district = ipJson.getString("district");
28 Location location = new Location(country, province, city, district); 29 Location location = new Location(country, province, city, district);
29 return location; 30 return location;
30 - } catch (UnsupportedEncodingException e) { 31 + } catch (Exception e) {
31 e.printStackTrace(); 32 e.printStackTrace();
32 return null; 33 return null;
33 } 34 }