Toggle navigation
Toggle navigation
This project
Loading...
Sign in
fangyeqing
/
xkl-interface
·
Commits
Go to a project
GitLab
Go to dashboard
Project
Activity
Files
Commits
Pipelines
0
Builds
0
Graphs
Milestones
Issues
0
Merge Requests
0
Members
Labels
Wiki
Forks
Network
Create a new issue
Download as
Email Patches
Plain Diff
Browse Files
Authored by
zhaoyue
8 years ago
Commit
d94cf728b7e819c30fa4fc2b166cf85dd892504d
1 parent
4040fb41
master
...
zhaoyue-dev4
ADD report location log
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
34 deletions
src/main/java/com/xkl/domain/Location.java
src/main/java/com/xkl/tools/HttpTools.java
src/main/java/com/xkl/tools/IPTools.java
src/main/java/com/xkl/domain/Location.java
View file @
d94cf72
...
...
@@ -4,10 +4,10 @@ package com.xkl.domain;
* Created by zhaoyue on 2017/4/15.
*/
public
class
Location
{
private
String
country
;
private
String
province
;
private
String
city
;
private
String
district
;
private
String
country
;
private
String
province
;
private
String
city
;
private
String
district
;
public
Location
(
String
country
,
String
province
,
String
city
,
String
district
)
{
this
.
country
=
country
;
...
...
@@ -16,6 +16,11 @@ public class Location {
this
.
district
=
district
;
}
@Override
public
String
toString
()
{
return
country
+
province
+
city
+
district
;
}
public
String
getCountry
()
{
return
country
;
}
...
...
src/main/java/com/xkl/tools/HttpTools.java
View file @
d94cf72
...
...
@@ -16,46 +16,49 @@ public class HttpTools {
/**
* 获取当前时间,10位
*
* @return
*/
public
static
String
getNow
(){
public
static
String
getNow
()
{
Calendar
c
=
Calendar
.
getInstance
();
//可以对每个时间域单独修改
return
String
.
valueOf
(
c
.
getTimeInMillis
()
/
1000
);
return
String
.
valueOf
(
c
.
getTimeInMillis
()
/
1000
);
}
/**
* 发送http请求
* @param url 请求url
* @param type 请求类型:GET,POST,DELETE
*
* @param url 请求url
* @param type 请求类型:GET,POST,DELETE
* @param params 请求参数:map类型
* @return
*/
public
static
String
requestByMapWithToken
(
String
url
,
String
type
,
Map
<
String
,
String
>
params
,
String
authorization
){
String
reqbody
=
getRequestData
(
params
,
"UTF-8"
).
toString
();
String
result
=
requestByString
(
url
+
"?"
+
reqbody
,
type
,
authorization
,
null
);
public
static
String
requestByMapWithToken
(
String
url
,
String
type
,
Map
<
String
,
String
>
params
,
String
authorization
)
{
String
reqbody
=
getRequestData
(
params
,
"UTF-8"
).
toString
();
String
result
=
requestByString
(
url
+
"?"
+
reqbody
,
type
,
authorization
,
null
);
return
result
;
}
public
static
String
requestByMap
(
String
url
,
String
type
,
Map
<
String
,
String
>
params
){
return
requestByMapWithToken
(
url
,
type
,
params
,
""
);
public
static
String
requestByMap
(
String
url
,
String
type
,
Map
<
String
,
String
>
params
)
{
return
requestByMapWithToken
(
url
,
type
,
params
,
""
);
}
/**
* 发送http请求
* @param url 请求url
* @param type 请求类型:GET,POST,DELETE
*
* @param url 请求url
* @param type 请求类型:GET,POST,DELETE
* @param reqbody 请求参数:String类型
* @return
*/
public
static
String
requestByString
(
String
url
,
String
type
,
String
authorization
,
String
reqbody
)
{
public
static
String
requestByString
(
String
url
,
String
type
,
String
authorization
,
String
reqbody
)
{
HttpURLConnection
con
=
null
;
String
result
=
null
;
try
{
con
=
getHttpConnection
(
url
,
type
,
authorization
);
con
=
getHttpConnection
(
url
,
type
,
authorization
);
//you can add any request body here if you want to post
if
(
reqbody
!=
null
){
DataOutputStream
out
=
new
DataOutputStream
(
con
.
getOutputStream
());
if
(
reqbody
!=
null
)
{
DataOutputStream
out
=
new
DataOutputStream
(
con
.
getOutputStream
());
out
.
writeBytes
(
reqbody
);
out
.
flush
();
out
.
close
();
...
...
@@ -64,7 +67,7 @@ public class HttpTools {
BufferedReader
in
=
new
BufferedReader
(
new
InputStreamReader
(
con
.
getInputStream
()));
String
temp
=
null
;
StringBuilder
sb
=
new
StringBuilder
();
while
((
temp
=
in
.
readLine
())
!=
null
)
{
while
((
temp
=
in
.
readLine
())
!=
null
)
{
sb
.
append
(
temp
).
append
(
" "
);
}
result
=
sb
.
toString
();
...
...
@@ -76,21 +79,21 @@ public class HttpTools {
}
private
static
HttpURLConnection
getHttpConnection
(
String
url
,
String
type
,
String
authorization
){
private
static
HttpURLConnection
getHttpConnection
(
String
url
,
String
type
,
String
authorization
)
{
URL
uri
=
null
;
HttpURLConnection
con
=
null
;
try
{
try
{
uri
=
new
URL
(
url
);
con
=
(
HttpURLConnection
)
uri
.
openConnection
();
con
.
setRequestMethod
(
type
);
//type: POST, PUT, DELETE, GET
if
(
type
.
equals
(
"POST"
))
if
(
type
.
equals
(
"POST"
))
con
.
setDoOutput
(
true
);
con
.
setConnectTimeout
(
60000
);
//60 secs
con
.
setReadTimeout
(
60000
);
//60 secs
con
.
setConnectTimeout
(
2000
);
//2 secs
con
.
setReadTimeout
(
2000
);
//2 secs
con
.
setRequestProperty
(
"Content-Type"
,
"application/json;charset=UTF-8"
);
con
.
setRequestProperty
(
"authorization"
,
authorization
);
}
catch
(
Exception
e
){
System
.
out
.
println
(
"connection i/o failed"
);
con
.
setRequestProperty
(
"authorization"
,
authorization
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"connection i/o failed"
);
}
...
...
@@ -100,7 +103,7 @@ public class HttpTools {
private
static
StringBuffer
getRequestData
(
Map
<
String
,
String
>
params
,
String
encode
)
{
StringBuffer
stringBuffer
=
new
StringBuffer
();
//存储封装好的请求体信息
try
{
for
(
Map
.
Entry
<
String
,
String
>
entry
:
params
.
entrySet
())
{
for
(
Map
.
Entry
<
String
,
String
>
entry
:
params
.
entrySet
())
{
stringBuffer
.
append
(
entry
.
getKey
())
.
append
(
"="
)
.
append
(
URLEncoder
.
encode
(
entry
.
getValue
(),
encode
))
...
...
src/main/java/com/xkl/tools/IPTools.java
View file @
d94cf72
...
...
@@ -12,14 +12,15 @@ import java.util.Map;
* Created by zhaoyue on 2017/4/15.
*/
public
class
IPTools
{
public
static
final
String
sinaIPGetter
=
"http://int.dpool.sina.com.cn/iplookup/iplookup.php
?format=json&
"
;
public
static
final
String
sinaIPGetter
=
"http://int.dpool.sina.com.cn/iplookup/iplookup.php"
;
public
static
Location
getLocationWithIP
(
String
IP
)
{
try
{
Map
<
String
,
String
>
params
=
new
HashMap
<
String
,
String
>();
params
.
put
(
"ip"
,
IP
);
String
url
=
sinaIPGetter
+
URLEncoder
.
encode
(
IP
,
"utf-8"
);
String
content
=
HttpTools
.
requestByMap
(
url
,
"POST"
,
params
);
params
.
put
(
"ip"
,
IP
);
params
.
put
(
"format"
,
"json"
);
String
content
=
HttpTools
.
requestByMap
(
sinaIPGetter
,
"POST"
,
params
);
JSONObject
ipJson
=
JSONObject
.
parseObject
(
content
);
String
country
=
ipJson
.
getString
(
"country"
);
String
province
=
ipJson
.
getString
(
"province"
);
...
...
@@ -27,7 +28,7 @@ public class IPTools {
String
district
=
ipJson
.
getString
(
"district"
);
Location
location
=
new
Location
(
country
,
province
,
city
,
district
);
return
location
;
}
catch
(
UnsupportedEncoding
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
null
;
}
...
...
Please
register
or
login
to post a comment